我有一个文本字段,当用户按下返回按钮时我想隐藏它。文本字段是在界面生成器中创建的,我在 .h 文件中添加了文本字段委托,并将文本字段的委托设置为文件所有者。
@interface ProfileEdit : UIViewController<UITextFieldDelegate>{
UITextField *textfield1;
UITextField *textfield2;
UITextField *textfield3;
}
- (void)viewDidLoad
{
textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 49, 164, 31)];
[textfield1 setDelegate:self];
[textfield1 setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:textfield1];
textfield2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 124, 164, 31)];
[textfield2 setDelegate:self];
[textfield2 setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:textfield2]
textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(20, 198, 164, 31)];
[textfield3 setDelegate:self];
[textfield3 setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:textfield3];
[super viewDidLoad];
}
我还在后台放了一个按钮,触发 touchupinside 事件
-(IBAction)hideKeyboard:(id)sender{
[textfield1 resignFirstResponder];
[textfield2 resignFirstResponder];
[textfield3 resignFirstResponder];
}
这工作正常,没有错误。但为此
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self hideKeyboard:nil];
return YES;
}
我在 main.m 中得到 EXC_BAD_ACCESS。我已经坚持了几天,不知道为什么会这样。