This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Diffrence between textfieldshouldendediting and textfieldDidendediting in iPhone
What is the diffrence between textFieldShouldendEditing and textfieldDidEndEditing methods? and when will use these methods.
From documentation:
textFieldShouldendEditing:
This method is called when the text field is asked to resign the first responder status. This might occur when your application asks the text field to resign focus or when the user tries to change the editing focus to another control. Before the focus actually changes, however, the text field calls this method to give your delegate a chance to decide whether it should.
textfieldDidEndEditing:
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.
So, the textFieldShouldendEditing:method will be called before textfieldDidEndEditing:method
textFieldShouldendEditing will call when your textField is about to end edit mode. It has a BOOL return type. If you set return NO than your textField will not be resignFirstResponder and remain in editing mode and textfieldDidEndEditing is not going to call.
while textfieldDidEndEditing this is method which tell you that your text field not in editing mode and your keyboard is down.
For more details plz refer UITextFieldDelegate Protocol
Hope this helps :)
textFieldDidEndEditing:
is called when the text field resigns as the first responder. So you can then get the value entered from the user.
textFieldShouldendEditing: is used to check if the key entered by the user should be displayed on the text field or not. So if you return NO the key is not displayed.
Imagine a label where you want to restrict the number of letters entered. You can use the above to check how many characters are entered and if they have reached the limit, return the value NO which basically doesn't take any new key strokes.
You can check the reference doc for more info.
Related
There are 2 forms in my program. When view controller A gets the focus and be active, view controller B will lost the focus and be inactive. Can anyone show me What the events are?
Thanks
Miken, it depends on what type of objects you are using for your "forms".
The simplest "form" to use is a UITextField, and in that case, events will be sent to your UITextField's delegate. In a lot of simple cases, you will designate the viewController that holds your UITextfield to be the UITextFieldDelegate. For more information on the methods that the delegate has, take a look at this: https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intf/UITextFieldDelegate
In this case, when the text field gets the focus (ie the user clicks on the text field to edit it) the delegate methods:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
will be called. I'm using this as a basic example, and not assuming too much here, but generally you should be looking into your "forms" delegate methods.
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.
Alright, so here is what I am trying to do. I have a UITextField. When I single tap it, I want to call one of my methods. When I double tap (tap it twice with 1 finger) it, I want to edit the text field (as though I had single tapped it on a normal UITextField.)
I am not sure how to go about this. I was thinking of sub-classing UITextField and trying to intercept the touch event as it is sent, but I can't figure out which methods to override to do this. Which methods should I override and how?
Or, if there is a better way to do this, let me know! I'm all ears, and not sure how to proceed.
This would involve several steps:
1.) Add a NSBoolean property that keeps track of whether the user has already tapped the field once recently (you get to decide what recently means here).
2.) Implement the textFieldShouldBeginEditing: method of the delegate assigned to your UITextField. If the user has tapped twice in quick succession (detectable by checking whether or not the boolean property is true), then return YES. If not, call your method, and then return NO.
How do I know when a user taps on the Join button on the UIKeyboard?
Also can this text be changed? I know there are several built options but I nee done that says 'Login' if I can.
I'm not a big fan of using UITextField delegate just for knowing when keyboard entry is completed by pressing the "return" on the keyboard (whatever it's labeled). UITextField actually works well with a Target/Action pattern which is much simpler and far more direct than using the delegate for this.
I've pointed this out before in an answer to "How to call a method when the Done Button in the KeyBoard is Clicked?". Please take a look at that for the details of using the traditional target/action pattern to know when keyboard entry is complete. Unfortunately, Apple's documentation isn't as clear is it could be on this subject.
UITextField delegate has its place, but if all you need is to know when entry is complete then using the delegate likely isn't the best approach. Use target/action.
For the UIReturnKeyType choices, see the returnKeyType property in the UITextInputTraits Protocol Reference.
You should set your delegate of your UITextField (I'm assuming that's a UITextField) and check for that delegate call :
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Also, you return a BOOL to tell the UITextField to do its default behavior or not. You can check the documentation here.
Also, the options Apple gives you for the UIReturnKeyType are your only options.