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

发布于 2024-11-07 07:23:50 字数 175 浏览 1 评论 0原文

我有 UITextField 当应用程序启动时它成为FirstResponder。然后,我有一个 UIButton ,按下它时 resignFirstResponder

如何用同一个按钮隐藏和显示键盘?是否可以?

I have UITextField which becomeFirstResponder when app starts. Then, I have a UIButton which resignFirstResponder when press it.

How to hide and show keyboard with same button? Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖个坑埋了你 2024-11-14 07:23:50

根据您的需求,以下代码应该具有相同的按钮,既隐藏又显示文本字段的键盘

if ([textField isFirstResponder]) {
    [textField resignFirstResponder];
} else {
    [textField becomeFirstResponder];
}

edit
您还可以在 if 循环中输入代码来更改按钮的标题。
放置更改按钮标题的代码的适当位置应位于 textField 的委托方法 -textFieldDidBeginEditing:-textFieldDidEndEditing:

Based on what you seem to need, the following code should have the same button both hide and show the keyboard for the textField

if ([textField isFirstResponder]) {
    [textField resignFirstResponder];
} else {
    [textField becomeFirstResponder];
}

edit
You can also put in code to alter the title of the button in the if loop.
The appropriate place to put the code to alter the title of the button would in the textField's delegate methods -textFieldDidBeginEditing: and -textFieldDidEndEditing:

一身软味 2024-11-14 07:23:50
-(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];
        }
}
-(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];
        }
}
看海 2024-11-14 07:23:50
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;
        }
}
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;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文