resignFirstResponder/endEditing causes bad access - iphone

I am creating an application which creates forms dynamically from the data available from web services.So I am adding a View programmatically each time when I need to present a page which may consist of a label for question,textfield for answer and a text area for comments.My problem is I have applied validations to the textfield in textfield delegate methods.And when I try to dismiss the keyboard by using return key , it leads to bad access with the following message
*** -[UITextField isKindOfClass:]: message sent to deallocated instance 0x7fb9f8f56390.
the delegate method is as follows:
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
NSLog(#"textFieldShouldReturn");
isKeyBoardPresent = NO;
if([textField becomeFirstResponder])
[textField resignFirstResponder];
return YES;
}
I have enabled zombie in my project.By that I got the above line.But I am not able to find out what the problem is.Please help me to solve this.

Try changing your if statement from:
if([textField becomeFirstResponder])
[textField resignFirstResponder];
to:
if([textField isFirstResponder]) {
[textField resignFirstResponder];
}

Related

keyboard does not showing with text fields in iPhone

I have a view with 2 text fields for the user inputs.But when clicking on those text fields,keyboard is not showing.I have connected the delegate and implemented the following method.username and password are my text fields.
- (BOOL)textFieldShouldReturn:(UITextField*)txts{
[username resignFirstResponder];
[password resignFirstResponder];
return YES;
}
Are they over released? Are they nil? Try assigning them to the first responder.
[textField becomeFirstResponder];
then implement BOOL method
-(BOOL)canBecomeFirstResponder {
return YES;
}
Are you sure you have set the delegate?
[textField setDelegate:self];
Did you connect them properly if you used them in XIB?

UITextField strange behaviour on resignFirstResponder

Already the second day and cannot figure out the problem,
I've UITabelView with Custom UICellViews, each custom UICellView consists of UILabel and UITextField.
Custom UICellView object allocs UITextField and UILabel in its init method and are released in dealloc.
The number of custom UICellViews in UITableView is 6.
The user scenario is following
When user clicks from from 1 to 5 UITextFields virtual keyboard opens and user types some text
When user clicks on the 6th UITextField if virtual keyboard is active, it should be hidden, and if it is hidden it shall not be displayed.
As implement UITextFieldDelegate protocol in my UIViewController class and set the delegate of each UITextField to self.
My delegate methods are following
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
[textField resignFirstResponder];
return NO;
}
}
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
/* Some code */
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
All the functions are properly !
So now, the virtual keyboard is never get hidden, why this happens ?
PS. Similar code has worked on iPhone but this issue exists on iPad.
You need to know which textfield was last used! so you can do [lastUsedTextField resignFirstResponder]
There is a dirty, but working trick.. you can make your textfield the new active UITextField and call resignFirstResponder in the next cycle immediately:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
// this will schedule keyboard dismissal for the current text field
dispatch_async(dispatch_get_main_queue(), ^{
[textField resignFirstResponder];
});
return YES; // -> make this one active
}
}
did you seted action for textField?
[YourTextField addTarget:self action:#selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
PS set any selector for any ControlEvent

Adding Done functionality to keyboard for UITextField

I have a view with two UITextField, one for username, one for password.
I added functionality so the usernameTF return button jumps to the passwordTF. I also made the return button on the passwordTF "Go" which I want to connect to a IBAction I have which will contain the actual login logic.
I tried hooking up the IBAction to the passwordTF's Did End On Exit method but that just seemed to be getting called when the usernameTF was making the passwordTF the first responder.
I'm using this code to jump from usernameTF to passwordTF:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.usernameTextField) {
[self.passwordTextField becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}
I tried this but it does't call the method when I press Go/Return:
[self.passwordTextField addTarget:self
action:#selector(loginButtonPressed:)
forControlEvents:UIControlEventEditingDidEndOnExit];
I also tried connecting the IBAction to the UITextField's Editing Did End method which calls the method fine when I press the return button, but it also calls the method when I click any UITextField other than the one I have connected with that method. Any fix?
You can try using this
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
if (textField == txtfEmail)
[txtfPassword becomeFirstResponder];
else {
[self login];
}
return YES;
}
Where [self login] is actually the login method being called. Validating login credential from Server or from Database. txtfEmail is Login Name and txtfPassword being the password field. Hope it helps
#RahulSharma is almost there, but not quite.
It has 2 issues addressed by the following approach:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
// [textField resignFirstResponder];
if (textField == txtfEmail) {
[txtfPassword becomeFirstResponder];
return NO; // Otherwise the "Return" char is posted onto next view
} else {
[self login];
}
return YES;
}
invoking resignFirstResponder is not necessary
returning NO will ensure that the CR will not be sent to the next control

iPhone: Possible to dismiss keyboard with UITextField clear button?

I'm wondering if there is a way to have the UITextField clear button 'always visible'
textfield.clearButtonMode = UITextFieldViewModeAlways;
doesn't seem to work
.. and if it is possible to dismiss the keyboard using the button?
Thanks in advance.
Like mentioned before it seems apple is setting the textfield focus after you clear the field.
The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
In your delegate, the function
- (BOOL)textFieldShouldClear:(UITextField *)textField
is called when the users wants to clear the textfield. If you return YES and call
[textField resignFirstResponder];
the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.
edit To make sure you really resign the responder, try doing it just a little later:
[textField performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:0.1];
The delay didn't work well for me. Instead I added an instance variable to the delegate:
BOOL cancelEdit;
Then in the delegate implementation:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (cancelEdit) {
cancelEdit = NO;
return NO;
} else {
return YES;
}
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
cancelEdit = YES;
return YES;
}
UITextFieldDelegate textFieldShouldClear
- (BOOL)textFieldShouldClear:(UITextField *)textField {
[textField] resignFirstResponder];
return YES;
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:
I discovered this odd behavior was caused by a competing gesture recognizer that resigned the first responder before the keyboard before textFieldShouldClear: was called. It seemed to be corrupting the first responder.
If you've set it up this way, ensure that cancelsTouchesInView on your gesture recognizer is set to YES. This way you shouldn't need to do anything special in the textFieldShouldClear: or textFieldShouldBeginEditing: delegate methods.

Keyboard not disappearing when i press "Done" on the iPhone

I am trying to implement a hello world application for the iPhone and i've worked through some. However i can't figure out how to get the keyboard to go away. I've seen this and no it doesn't help (i have resignFirstResponder in my code). I have connected the relevant textfield to the file's owner as a delegate. Here is the code that determines whether the keyboard should dissapear:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textField)
{
[textField resignFirstResponder];
}
return YES;
}
I'm sure this should be ridiculously obvious, but i cant find the answer. Thanks in advance for your help!
First off, just to clarify: you should connect the text field's delegate to the file's owner, not the file's owner's delegate to the text field. That may sound confusing, but you can check it easily by selecting your text field in Interface Builder and checking that its "delegate" connection points at the file's owner.
Next, what happens if you take out the if statement in your code? Linking the text field's delegate to the file's owner and then changing your code to:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
should produce the desired results.
If it still doesn't work, check that the file's owner's class is the same as the class you have that method implemented in. For example, if the code is in RootViewController.m, then you want to specify that the file's owner is an instance of RootViewController in Interface Builder.
your code
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textField)
{
[textField resignFirstResponder];
}
return YES;
}
is sort of fail.
You didn't call resignFirstResponder to the parameter of the delegate method, you called it to what I assume is an instance variable. This should have been written as follows:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == [self textField]) {
[theTextField resignFirstResponder];
}
return YES;
}