Why use resignFirstResponder() - ios

I am new to stackoverflow and iOS app development using Swift. When I want to dismiss the keyboard in my apps I connect the uitextfield in viewcontroller.swift and create an action with the event DidEndOnExit. Then in that method I call the self.resignFirstResponder() method. This is what everybody says I should do. Here is my code in viewcontroller
#IBAction func dismissKeyboard(sender: AnyObject) {
self.resignFirstResponder()
}
Now when I was searching to find out what this method does I found that when I choose the textfield then the keyboard appears and that textfield becomes the first responder and by calling this method it resigns the first responder and the keyboard disappears. The thing is that if I don't use this method the keyboard still disappears. All I need to do is to create the action with the DidEndOnExit event. Am I missing something here? Here is the code without the method;
#IBAction func dismissKeyboard(sender: AnyObject) {
//self.resignFirstResponder()
}
If someone can enlighten me that would be great! :)
Thanks in advance!

maybe you enabled "Auto-enable Return Key" in the Interface builder?

The only thing that could come to mind is that you have an auto enabled return key. You also said it wasn't enabled, so maybe it has something to do with the rest of your code in the ViewController. Also, check any properties that you could have been playing with to get it to work, like you said you are new to swift so you we probably playing around with getting the keyboard to dismiss.
Sometimes Xcode can be a little buggy, if you can't solve the problem or get the right answer my advice is make a new project and add just the keyboard dismissing code in.
I hope this helps you out a little not seeing the rest of the code, screen shots and etc. make it so I can only give my thoughts or past experiences!
Good luck! Have fun programming in swift.

Related

SkyFloatingLabelTextField auto move label up on focus

I am a newbie to this! I am using Swift 4 and I have configured the plugin called SkyFloatLabelTextField.
I was wondering if someone else has solved how to auto move the label up if the cursor is on focus? I want the label to move up the moment you select a text field. At the moment you have to type for the label to move up.
Thank you.
You can achieve this functionality by subclassing SkyFloatingLabelTextField and managing title visibility.
final class MovingTitleOnFocusTextField: SkyFloatingLabelTextField {
override func becomeFirstResponder() -> Bool {
setTitleVisible(true)
return super.becomeFirstResponder()
}
override func resignFirstResponder() -> Bool {
setTitleVisible(hasText || hasErrorMessage)
return super.resignFirstResponder()
}
}
If you use SkyFloatingLabelTextFieldWithIcon you have to subclass that as well, but same idea.
UPDATE 2
I have forked the project and did the necessary changes. You can check it out here. I also did a Pull Request to the original author so this functionality will maybe be added to the original project.
To use the behavior simply set myFloatingTextField.isAnimationOnTouch 0 true. This will cause the animation to happen right when you tap in the textfield.
Happy coding!
UPDATE 1
So the question was more related to the behavior of the SkyFloatingLabelTextField. Sorry for misunderstanding!
Following the very first bunch of code on the github page, the label will move up as soon as you type. So you want it right from the touch?
Then you should fork the project, and adopt the code from
#objc open func editingChanged() {
updateControl(true)
updateTitleLabel(true)
}
to achieve it on the touch. This can be done in the delegate method func textFieldDidBeginEditing(_ textField: UITextField)
Once you have done this, I think you will be able to have the label move up once the user touches the textField.
Original Answer
This is not depended on the Labels you are using, the workflow is the same for every kind of view: The Keyboard appears and the textField should move up so the keyboard does not obscure the textField.
To achieve this, please have a look to this SO answer: Move textfield when keyboard appears swift
.
I don't want to copy the whole code in there, just point you into the right direction.
Happy coding!

IOS swift 3 crash for UITextFields editing and responder

I'm facing a strange problem with dealing with UITextFields and keyboard visibility.
The purpose of my current controller is to let user edit some datas but also read some others.
So I decided to include few textfields in it and when user modify some data a button appears and he can press it to send modifications. But to read other datas keyboard has to hide.
I made some researches to find a working way to do so like :
self.view.endEditing(true)
currentTextField.resignFirstResponder()
even create an "hidden UITextField" and ask it to becomeFirstResponder when process is done
None of these tries ended with a hide of the keyboard but ended with Optional value crash
I'm using Swift 3 and XCode 8.2.1 right now
If someone has a clue, thanks by advance
Your view controller needs to subclass UITextFieldDelegate. Implement the functions that are defined by this protocol and you should have the control you need. And don't forget to set myTextfield.delegate = self -- or whatever object implements the protocol.
An example would be "DidEndEditing" and similar.
If you already did this please provide some code for further diagnosing your issue.
Thanks for your answers,
I implemented UITextFieldDelegate from begin of code and all my TextFields use editingDidBegin and editingDidEnd and crash occured when I called the code below from the result of an API Call
self.view.endEditing(true)
I just found a way to succeed the hide of keyboard using this code before the API call. The sad side is that this new way avoid the possibility to keep keyboard visible if the call fails.
self.view.endEditing(true)
This line should not crash. It just resign responder from UITextfield. Can you please give me detail about your crash? What is written in log console when crash happens. as far as I know self.view.endEditing(true) will definitely fulfil your requirements.

The handler of a UIAlertAction is a bit too late - how can I make it immediate?

I am trying (...) to add a sound effect to the buttons added to a UIAlertController. I fire a sound effect in the handler, but this actually is a bit too late. The sound fires like 0.5 seconds too late. I want the sound to fire as soon as the alert is about to dismiss, not after it has dismissed. With UIAlertView this was possible to handle using alertWillDismiss... rather than alertDidDismiss.
Did I miss something?
No, you didn't miss anything. The functionality you're looking for is not provided by UIAlertController. Consider providing your own presented view controller, over which you'll have the kind of fine control you're after.
I used Patrick Goley's suggestion, namely to subclass UIAlertController and override viewWillDisappear. Worked great for me.
//
// ImmediateClickAlertController.swift
//
// This subclass of UIAlertController plays a click immediately whenever it is dismissed (i.e. when a button is tapped).
// This fixes an issue when trying to play a click in an attached UIAlertAction, which does not happen until after its view disappears.
import AudioToolbox
class ImmediateClickAlertController: UIAlertController {
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
// play a click
let pressKeySystemSoundID: SystemSoundID = 1104
AudioServicesPlaySystemSound(pressKeySystemSoundID)
}
}
A bit of a hackery, but perhaps you could:
Try and grab a reference to the alert's button (by traversing the -admittedly private- view hierarchy tree), and
Use KVO to detect any changes to its selected and/or highlighted properties (my own experience is that selected is not reliably observable, while highlighted is).
...but all of this is quite fragile, not elegant, maight break in a future release of the OS and/or get you rejected from the app store...?
So you're best option (even if the most laborious) is to roll your own modal view controller:
Apple's documentation on presenting modal view controllers.
Demo project I made following the above docs (a custom "UIAlertController look-alike" with an embedded UIActivityIndicator - for use during long, asynchronous processes):

How do I present a JSQMessagesViewController with the keyboard already up?

I'm using an open source messaging UI library for an app that I'm building. When users start a new conversation I want the "chat view" to appear with the keyboard already up and the cursor on the textfield (similar to most existing chat applications). Is there a way to force the JSQMessagesViewController to appear with the keyboard already up?
I tried implementing this using:
self.keyboardController.textView.becomeFirstResponder()
However, this caused the keyboard to pop up immediately when the view was presented... yet the toolbar would lag behind by about a second (not too much, but painfully noticeable). In addition, this solution seems to disable the keyboard from being dismissed using a downward gesture as it normally does.
Is there something I'm missing that solves this out of box? Or will I have to modify the library to get this bit of functionality... and if so, where do I begin doing that?
try to do like this
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[myTextField becomeFirstResponder];
}
--> It will make focus on myTextField and open the keyboard automatically. Hope it may help you.
This worked for me. It's in swift, but should be easy to translate.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(false)
self.inputToolbar!.contentView!.textView!.becomeFirstResponder()
}
I wanted this with a button click so I added:
[self.inputToolbar.contentView.textView becomeFirstResponder];
If you want yours when it loads add:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.inputToolbar.contentView.textView becomeFirstResponder];
}
This maybe too late, but in my case I forgot to call super.viewDidAppear(animated) which caused text view to lag behind keyboard. Once I added that call, problem went away.

UITextField is not responding

I'm experiencing some problems with UITextField inside a UITableViewCell.
Things are working well until I open the iOS media player, watch a short movie and going back to my view.
Then, I'm trying to click on the UITextField, but nothing happens and it does not responds to my clicks.
In the same screen (it's a UITableView), I have a switch button (in another row), which is working fine after switching views.
My view is implementing the UITextFieldDelegate protocol and textFieldShouldReturn in particular.
My implementation of textFieldShouldReturn is a simple call to : [textField resingFirstResponder]
I'll appriciate any thoughts or ideas why it happens and how to solve it.
Thanks!
koby
I had this same problem, what fixed it for me was to make sure to return the correct height for the table view cell. Check your heightForRowAtIndexPath function - even though you can see all the objects in your cell they may be outside of a clickable/tappable area.

Resources