IOS swift 3 crash for UITextFields editing and responder - ios

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.

Related

Why use resignFirstResponder()

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.

TableView didSelectRowAtIndexPath not called

So I have added a UIUITableView to a UIViewController. I can't use a UITableViewController for reasons I don't need to explain since it will be unnecessary information. Anyway, I have set the delegate, and the data source to this viewController. I've added the delegate and datasource protocols as well. The cells are populated correctly, so the datasource is working fine. I can also scroll so it all works fine.
However, I can't get the didSelectRowAtIndexPath to trigger. It SHOULD trigger, but doesn't. I've read and a lot of issues with this can be correlated to a UIGestureRecognizer, but I haven't implemented one. I also use the standard UITableView, so not a custom made one.
If I long press the cells (3-4 sec) then it gets triggered as it's supposed to. This suggests that there is some issue with another view or something absorbing the tap gesture, which I have no control over. How would I solve this?
No, it's not didDeselectRowAtIndexPath.
Yes all delegates and datasources are correct, since I can get the delegates/sources to trigger.
Yes, Single Selection is set on the TableView in the inspector.
Yes, everything has user interaction enabled.
If I just copy the code over to a UITableViewController it will work just fine, but right now that is not an option, I'm afraid. Anyone got any ideas on how to solve this? Most people who've had this issue has either had the issues in the list above, or added a UIGesture on top of the UITableView - I haven't.
I want to start by saying that I appreciate all the answers here provided, it gave me a lot of things to try out so I learned a lot - thanks! None of your suggestions worked, but simply because I'm a complete idiot. I said in the post that I did NOT implement a UIGestureRecognizerwhich I didn't...in that class, but in its super class. So I DID in fact implement it, but in a class that this ViewController was a subclass off. The only reason I didn't remember it was because I haven't looked at that super class for weeks.
Someone did suggest it in the comments that I should check for it, and I did and I was already certain I didn't implement one, so I quickly dismissed it. But now, after about 4 hours of debugging and recreating the project, adding things one by one, I eventually realized that the only thing that differed at this point was the Super Class, and the first piece of code I see when I open the file up was a GestureRecognizer...
So keep this in mind in the future everyone - I know I will. Thanks again for the help!
Sincerely,
The complete idiot.
Make sure you don't add any controller in that cell, which covers the entire cell and also the user interaction of that controller is enabled.
Due to that controller's user interaction enabled the tap action is taken by that controller and when you long press that cell, that cell will receive your tap.
Example :
UIView *_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _cellWidth, _cellHeight)];
[_view setUserInteractionEnabled:YES];
[cell.contentView addSubview:_view];
In the above case that view got your tap instated of the cell.

Problems with UITextField & Keyboard Management in iOS

(I'm targetting iOS 2.0 for the time being.)
I have a class which subclasses UITextField and implements the UITextFieldDelegate protocol.
At first, I did not set the class as its own delegate, and when I clicked in the field, the keyboard popped up automatically. So far, so good.
I send the setReturnKeyType: message with UIReturnKeyDone, so it changes the bottom-right button to say Done.
The problem was, when I clicked Done or pressed [Return], nothing happened - the keyboard wouldn't go away.
I tried adding self as an observer for the end editing notification, but it never got called when Done was clicked; from a Google search it seems that only gets fired when the field resigns as the first responder - which is the bit I can't get to happen.
I then found answers on here that suggested adding self as the delegate and handling textFieldShouldReturn:. The problem is, as soon as I add self as the delegate, the keyboard no longer pops up when you click the field (and it doesn't gain focus) - it seems it isn't becoming first responder.
I tried handling textFieldShouldBeginEditing: and returning YES (docs say that should be the default if not overridden), and that got called, but made no difference. In my ignorance I tried [textField becomeFirstResponder] and was rewarded with a stack overflow (now I know that is called in response to trying to become first responder).
I'm now thoroughly stuck!
Can anyone help? I just want the keyboard to go away when the user clicks Done or presses [Return].
Are you using an xib or doing it programatically? If you are using an xib then you may have forgotten to connect the delegate in File's Owner.
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Try This one.
My fault. Turns out you're not supposed to set a text field's delegate to itself - there are complications due to unhandled selectors being forwarded to its delegate. If it doesn't respond to a selector the message goes to its delegate... which is itself, which we already know doesn't respond. Hence, an infinite loop.
Using a separate object as the delegate worked perfectly.
Hopefully this will help someone else avoid the same problem.

custom input view keyboard functionality

I wanted to ask a quick question just to make sure I am not missing anything simple before I implement a more difficult method. I need to create a custom keyboard for an iPhone application. This I have already done by creating a view with the buttons, using a custom input view and it displays exactly like it should. Now most of the buttons are standard numbers which need to update a UITextField in the screen that called the keyboard. Does anyone know a simple way to do this? I assume there has to be a built in function that the keyboard uses to send the information but I haven't been able to find any reference to it. Otherwise I will have to go the more difficult route. If anyone has a simple way to do this I would appreciate it. I haven't worked with custom keyboards before.
You won't be able to do it the same way that Apple does it, as their keyboard is basically an input device, globally.
I recommend you just append the data in your button press multiplex method. Here's an example:
NSString *appendThisText = #"subtitle";
self.myTextView.text=[NSString stringWithFormat:#"%#%#", self.myTextView.text, appendThisText];
Custom keyboards are simpler than you realise.
UITextField conforms to the UITextInput protocol. That's a bit of a red-herring because this protocol provides all the really complex stuff like selecting text and so on. But UITextInput itself conforms to UIKeyInput. This is your friend.
The key UIKeyInput methods are:
- (void)insertText:(NSString *)text;
- (void)deleteBackward;
Your keyboard class should have a delegate (which points to the textfield that the keyboard is operating on) and you simply call these methods to insert and delete text.

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