【问题标题】:Adding and removing keyboard添加和删​​除键盘
【发布时间】:2012-01-03 01:52:47
【问题描述】:

我有一个文本字段,当用户点击它时,他们将看到键盘。键盘上有一个GO 按钮,我想给它写一个action 事件。

1.) 当用户点击这个按钮时,我如何写一个action

2.) 当键盘打开时,当用户点击背景时,我需要键盘消失,我该如何以编程方式做到这一点?

我没有要演示的代码,我只添加了一个texfield,所以一旦点击,键盘就会默认出现

【问题讨论】:

    标签: iphone objective-c cocoa-touch iphone-softkeyboard


    【解决方案1】:

    要在返回文本字段/Go 按钮时执行一些操作,请使用以下代码

    -(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        [theTextField resignFirstResponder];
    
        //call Method when the GO button is pressed   
    
        return YES;
    }
    

    当用户触摸背景和键盘时应该返回 - 为此,请编写以下代码

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [textFiedl resignFirstResponder];
    
    }
    

    希望您的问题由此得到解决。

    【讨论】:

      【解决方案2】:

      只要 UITextField 或 UITextView 是第一响应者,就会出现文本字段。您可以通过调用 becomeFirstResponder 手动“显示”键盘或通过 resignFirstResponder“隐藏”它。

      在您的情况下,请查看 UITextFieldDelegate 参考;当用户点击“GO”时,将调用 textFieldDidEndEditing: 回调。在此方法中,您应该在文本字段上调用 ​​resignFirstResponder 以隐藏键盘。

      【讨论】:

        【解决方案3】:

        要在您触摸背景时隐藏键盘,您可以写[txtName resignFirstResponder];,其中txtName 是TextField 的引用名称。

        【讨论】:

          猜你喜欢
          • 2013-05-06
          • 2015-11-01
          • 2022-06-14
          • 1970-01-01
          • 2021-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-20
          相关资源
          最近更新 更多