I'm currently developing an iPhone app where I have two UITextField's in one View Controller.
I have a method that is called when the keyboard appears (keyboardWillShow), and one that is called when it disappears (keyboardWillDisappear).
Now if the user touches the first field the keyboardWillShow method is called, but if he now touches the second field without touching the background before, keyboardWillShow of course is not called again, because the keyboard is already here.
I can also not use textFieldDidBeginEditing:(UITextField *)textField because it's also not called when you change field's directly.
Now how can i call keyboardWillShow again if he touches the first and the second one without letting the keyboard disappear between??
I found out the problem was i didn't set the delegate of the textfield's properly.
After setting #interface ViewController : UIViewController <UITextFieldDelegate>in my .h file and textfield2.delegate = self in the .m file, textFieldDidBeginEditing: was called when i changed the textfield's directly.
Related
I want my UITextField's keyboard to stay open for the entire time I use this view controller. I don't want it to only open when my user touches the text field. In order to do this, I was hoping I would call the textFieldShouldBeginEditing method by doing this:
EDIT: thanks everyone, I just noticed I called my UITextField a UIImage field for some reason in the interface.
The textFieldShouldBeginEditing delegate method is not something that you call from your code. The OS calls the method when that particular event occurs, and you put code in there to run when the event is fired (similar to putting code in viewDidLoad for your view controller).
To show the keyboard whenever the view controller appears, simply call the UITextField's becomeFirstResponder method in the view controller's viewDidAppear method like this:
[self.myTextField becomeFirstResponder];
Don't forget to create an IBOutlet parameter for the UITextField, link it in Interface Builder, and replace self.myTextField above with the outlet that you created.
You should trigger your textview in viewDidAppear method:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.myTextField becomeFirstResponder];
}
I have multiple instances of a custom class that takes inputs from keyboard. You can think of UITextField (but they are not UITextField, they are NSObject). However, they all have a property UIControl *control.
These objects are instantiated and put into an array (orders matter), and they are put on the screen in the same order.
Scenario 1: User tabs on the first object, it becomes the first responder. User taps on another object (from the same class) and that becomes the first responder. No problem.
Scenario 2: User tabs on the first object, it becomes the first responder. User taps on the TAB button of the keyboard (iPad or iPhone or wireless keyboard), I want the next object in the array becomes the next responder. iOS picks randomly [? or with some logic not clear to me] another object which is not in the same order as I want.
Problem: Because these objects are NSObjects, how can I intercept the transition to the next object. I tried using tags or tracking who is the first responder, but the problem is, if user taps on an object out of order, it is fine - I don't want to intercept that. I only want to intercept transition from one object to anther only if it is through tapping on TAB (or Next or Return) button of keyboard.
Any idea? Thanks.
You can set your custom class to have something like this
#interface testClassButtonSub : UIButton
#property (weak,nonatomic) IBOutlet UIButton *nextButton;
#end
Then you can even use the interface builder to set which will be the next responder for when a certain action is taken. (an user presses the return when inside a textfield in your custom class)
For the return you have to declare your viewcontroller as the delegate of the specific textview.
First you set the viewcontroller header like this:
#interface RegisterViewController : UIViewController <UITextFieldDelegate>
then you set the delegates in the implementation
// Set Delegates of the Text Fields
eMail.delegate = self;
userPassword.delegate = self;
userNickname.delegate = self;
and you use this delegate method to jump to the next object
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// Jumping code here
return NO;
}
HOWEVER in your case your textfield must be inside your object, so you have to make THAT object the delegate, and in that object's implementation jumping code add the
[thisObjectsTextfield becomeFirstResponder];
I've searched in the web but didn't found anything similar to what I want. So,I am creating an application and I need to recognize when the user leaves a specific UITextField,more clearly.when the user enters a value in the UITextField and after touch outside to dismiss the keyboard, I need to recognize that the UITextField has lost activity for,after I perform an action.
Is this possible?
Look up UITextFieldDelegate in the Apple docs. Specifically the methods textFieldDidEndEditing: and textFieldShouldReturn:. Hook up the specific UITextField to an outlet and assign its delegate to your viewController. Then in the delegate method, if you need to make sure it's a specific text field, compare it to the IBOutlet.
write UITextFieldDelegate in .h file then after include the following method in .m file.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
I have created a custom keyboard and I have two text fields.
I am calling [firstTextField becomeFirstResponder] in my viewDidLoad
to have my keyboard visible.
How can I know which text field is currently active so that I write what the user is typing from the keyboard to the respected textField?
I have tried - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField but it is not being called
any idea?
You should be able to use isFirstResponder to determine which of your two UITextFields is currently active.
if ([firstTextField isFirstResponder]) {
...
}
else {
...
}
To get textFieldShouldBeginEditing to be called, you need to set the delegate outlets for both your text fields to whatever view controller (or wherever) the textFieldShouldBeginEditing method lives in.
You can set the delegates programmatically (e.g. firstTextField.delegate = self;) or via the XIB file.
And your intuition is correct, once textFieldShouldBeginEditing gets called, you will know (from the textField parameter) which field the user is currently typing in.
My view has two UITextFields and a UISwitch. If a user is edits a textField, and then immediately touches the switch (without pressing return), the text is left as they typed it, without AutoCorrect.
If I know which textField they were typing in, I can force the autocorrect to complete by calling [textField resignFirstResponder]. But the user could be typing in either textField, so I don't know which one to call.
How can I get around this? Is there a way of detecting which textField was being used? Or something simpler I haven't thought of?
One lovely way of doing this without having to keep track of which field is active:
// This causes the current responder (eg. an input field) to resignFirstResponder and
[self.endEditing:YES];
Replace [self.view endEditing:YES] with the below one...
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
The uitextfielddelegate methods are called for the textfield on which the editing is in progress. So that way you needn't be facing the problem of detecting which text field is being edited.
So implement the uitextfielddelegate methods and assign the delegate of the text field to the class where you implement the methods and handle the responses in them.
The methods which you should be interested in are:
textFieldDidEndEditing:
Tells the delegate that editing stopped for the specified text field.
- (void)textFieldDidEndEditing:(UITextField *)textField
Parameters
textField
The text field for which editing ended.
Discussion
This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing.
Implementation of this method by the delegate is optional.
Availability
Available in iOS 2.0 and later.
Declared In
UITextField.h
You may keep track yourself which one is the current one, by using the textFieldDidBeginEditing delegate.