如何使用相同的按钮隐藏和显示键盘

时间:2011-05-13 06:51:24

标签: iphone button keyboard uitextfield

我有UITextField {app}启动时becomeFirstResponder。然后,我有一个UIButton resignFirstResponder按下它。

如何使用相同的按钮隐藏和显示键盘?有可能吗?

3 个答案:

答案 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;
        }
}