Xcode UITextField textFieldDidEndEditing crashing

时间:2015-07-28 16:04:57

标签: ios objective-c xcode

In my application when the TextField loses focus I am checking the values as follows.

- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag==1){
    [self CheckUser:textField.text];
} else if (textField.tag==2){
    [self CheckEmail:textField.text];
} else {

}

}

It is working fine, no problem in it.

But when I dismiss the Modal form and move to the ViewController, it is crashing. I am using the below code for dismissing the Modal View Controller.

[self dismissModalViewControllerAnimated:YES];

If I remove the code - (void)textFieldDidEndEditing:(UITextField *)textField it is working perfectly without crash.

Can anyone help me.

1 个答案:

答案 0 :(得分:0)

确保您在文本字段的头文件中设置了UITextField委托:

@interface ViewController : UIViewController <UITextFieldDelegate>

然后,释放文本字段的isfirstResponder属性。请使用以下代码作为示例。

#pragma mark - UITextField Delegate Methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"Search for tag: %@", self.searchField.text);

    if (textField == self.textField1)
    {
        [self.searchField1 resignFirstResponder];

        NSLog(@"TextField1: %@", self.textField1.text);
    } 
    else if (textField == self.textField2)
    {
        [self.searchField2 resignFirstResponder];

        NSLog(@"TextField2: %@", self.textField2.text);
    }

    return YES;
}

修改

此外,如果您的视图层次结构嵌入在UINavigation控制器中,则忽略显示的模态视图:

// Apple's Description: Dismisses the view controller that was presented modally by the receiver.
[self dismissViewControllerAnimated:YES completion:nil];

这将删除最顶层的View Controller。 ^^^ 编辑 ^^^

最后,如果它仍然不起作用,请设置对TextField IBOutlets的强引用,并在.delegate中设置每个viewDidLoad属性。