我有UITextField
{app}启动时becomeFirstResponder
。然后,我有一个UIButton
resignFirstResponder
按下它。
如何使用相同的按钮隐藏和显示键盘?有可能吗?
答案 0 :(得分:2)
根据您的需要,以下代码应该使用相同的按钮隐藏并显示textField的键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
} else {
[textField becomeFirstResponder];
}
修改强>
您还可以输入代码来改变if循环中按钮的标题。
在textField的委托方法-textFieldDidBeginEditing:
和-textFieldDidEndEditing:
答案 1 :(得分:0)
-(IBAction) buttonPressed:(id) sender {
UIButton *button = (UIButton *) sender;
button.selected = !button.selected;
if (button.selected) {
// show the keyboard.
[textField becomeFirstResponder];
}else {
// Hide the keyboard.
[textField resignFirstResponder];
}
}
答案 2 :(得分:0)
mytest.h
IBOutlet UITextField *myTextField;
IBOutlet UIButton *myButton
- (IBAction) showHideKeyBoard:(id)sender;
mytest.m
-(IBAction) showHideKeyBoard:(id) sender {
if (myButton.selected) {
// show the keyboard.
[myTextField becomeFirstResponder];
[myButton setTitle:@"Hide" forState:UIControlStateNormal];
myButton.selected = NO;
}else {
// Hide the keyboard.
[myTextField resignFirstResponder];
[myButton setTitle:@"Show" forState:UIControlStateNormal];
myButton.selected = YES;
}
}