Hi I have a textfield to which I have assigned a UIPickerView as inputView. The UIPickerView has a method called using addTarget:action:forControlEvent: which I want to bring up an alertView to ask the user if this change is what they want.
Currently, I have the main view of the view controller as a UIControl, so when the background is tapped during editing I call [self.view endEditing:YES];
I want to bring up a UIAlertView when I end editing my textfield, I am playing with the control event. UIControlEventValueChanged doesn't work well because it brings up an alert view every time the wheel is spun and released, not only on exit. The other control events like UIControlEventDidEndEditing/OnExit don't do anything. What am I doing wrong?
I figured it out.
Even though the UIControlEventValueChanged worked on its own, I needed to hook up my textfield to my action method through interface builder. For some reason it did not work through code with the UIPickerView.
Related
I have a ViewController with a TextField for user input and a Quit button in a NavigationBar.
Normally, the user inputs text into the textField and the input is processed in textFieldDidEndEditing. If the Quit button is tapped before editing starts, then it segues correctly to the parent ViewController.
However, if editing has started on the textField and then the Quit button is tapped, textFieldDidEndEditing is still called, which I don't want to happen.
I have tried using textFieldDidEndEditing:reason: but the returned reason in both cases is UITextFieldDidEndEditingReasonCommitted. It seems that UITextFieldDidEndEditingReasonCancelled is only valid in tvOS not iOS.
How else can I detect that the user has Quit and prevent textFieldDidEndEditing from running to completion?
You can't prevent textFieldDidEndEditing from being called. Dismissing the view controller dismisses the text field. Since the text field is no longer being edited, the delegate method will be called.
One option you have is to set a flag when the user taps the Quit button. Then in your implementation of textFieldDidEndEditing you can check if that flag it set or not and act accordingly.
I know that the view controller must be firstResponder in order for the inputAccessory to stay at the bottom. I am using a custom inputView / keyboard. I can manually dismiss it with a done button by removing the inputView but not resigning first responder. However when I enable the interactive drag to dismiss on my scrollview, the code automatically resigns first responder. So how can I use the interactive drag to dismiss and yet keep my viewcontroller as first responder? Anyone done this before? I thought maybe it is not possible and that I may need to make my own interactive drag to dismiss using a gesture recognizer.
More info:
I have a button that swaps between standard keyboard and my custom one. I have seen dismissing these cause 2 keyboard did dismiss notifications. I thought I could become firstResponder in the keyboardDidHide method but this didn't work well since I couldn't tell the difference between when I manually dismissed the keyboard and when the interactive drag does it. This matters because I don't need to reload the input view or become first responder when I manually dismiss because I took care of it already.
Any suggestions would be amazing. I am trying to use inputView and inputAccessoryView on the UIViewController level.
Well after a day of pulling my hair, I have an answer.
Using the canResignFirstResponder of my viewcontroller did the trick. In viewWillAppear I set a BOOL responderOverride = YES;
In viewWillDisappear I call
responderOverride = NO;
[self resignFirstResponder];
When the interactive drag on the scrollview tries to resignFirstResponder, canResignFirstResponder returns no which prevents my viewcontroller from resigning and keeps my input accessory retained and sitting at the bottom of the screen.
There is a lot of other code with reloading input views but since the real question was how to force a controller to stay first responder so we don't lose our input accessory view, then this solution works.
override var canBecomeFirstResponder : Bool {
get {
retrun true
}
}
This works for me
I'm new to iOS development and have recently learned that to make the on screen keyboard disappear we must always call resignFirstResponder on the text view. This causes the text view to relinquish it's first responder status, and hence the keyboard disappears, since there is no need for the text view to respond.
However I also noticed that there's the becomeFirstResponder method to make a view a first responder. However, this method is never called on the text view. So when/how does a textview become first responder when that method is never called?(at least, by me. I'm unsure if it is called elsewhere in the system)
My theory is that is has to already be a first responder before it can resign the first responder status.
firstResponder status is automatically handled for you when a user taps on the text field. So long as user interaction is enabled for the UITextField/UITextView, the keyboard should appear when tapped.
You can monitor for it using the delegate method textViewDidBeginEditiing or, more broadly, by listening for keyboard appearance notifications (UIKeyboardWillShowNotification and UIKeyboardDidShowNotification).
Further, there are ways to dismiss the keyboard without the need of calling the corresponding method resignFirstResponder (such as calling endEditing: on a container view, or setting a scroll view's UIScrollViewKeyboardDismissMode).
Note: In the simulator, it is possible that the keyboard still doesn't appear even if all is correctly working. In that case you just want to make sure keyboard hardware is toggled (CMD+K) for the simulator.
You call becomeFirstResponder yourself if you want to give a UITextField focus and show the keyboard.
This is useful for view controllers that are basically forms. For example, if a user presses a "Sign Up" button, you might push a view controller with a couple of text fields and call becomeFirstResponder on the first one to automatically give it focus and open the keyboard.
I have a ViewController that is just a textfield, a next button, and a back button. I want the text field to always be editable while having the keyboard always present. I also want to customize the keyboard to be my own, but that will come once I figure this part out.
EDIT: The keyboard in my case will only actually be a keypad with 10 digits and a backspace key
What is the best way to go about this? I've been working around with having a UITextField that works with a custom keyboard view, and then make that the first responder when the view loads, but maybe there are better ways.
Thanks in advance!
To make a UITextField always use the keyboard...
In the viewDidAppear or viewWllAppear function do this...
[self.textField becomeFirstResponder];
This will make the keyboard appear and the textField respond to the input.
To dismiss the keyboard you have to run...
[self.textField resignFirstResponder];
As long as you don't run this it will keep keyboard focus.
In the view controller's viewWillAppear: method you can call becomeFirstResponder on the text field. This will make the keyboard appear automatically when the view controller appears. As long as there is no other way to dismiss the keyboard, that is all you need.
Of course on the iPad there is a button on the keyboard to dismiss it. If you want to stop that button from working then implement the following delegate method:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return NO;
}
And in case you want to really have your own keyboard. create a view properly sized and beautified ;).. and put in the textFiled's inputView property.
textField.inputView = your custom keyboard view
Cheers.
In a brand new iOS project using Xcode 4.3.2, if a button has an Action created using the Interface Builder, and it prints out "button is tapped on", then all works fine.
However, when the main view is added a UITapGestureRecognizer for a single tap, then even if the button is tapped on, the main view is thought to be tapped on but the button Action won't be called.
Is there a way to make this work? (is it recommended to not use Button Action but use a UITapGestureRecognizer for the button? But if it was a slider, then we do need the Action)
Exclude the button in [UIGestureRecognizer delegate] or set cancelsTouchesInView to NO.