用swift编程语言隐藏文本字段的键盘 [英] hide keyboard for text field in swift programming language

查看:128
本文介绍了用swift编程语言隐藏文本字段的键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Objective-C的经验不多。我想使用Swift编程语言隐藏文本字段的键盘。



我也试过这个

  func textFieldShouldReturn(textField:UITextField!) - > Bool //按下'return'键时调用。返回NO忽略。 
{
返回true;
}

但是当我点击返回时,该方法没有被触发。任何人都有运气吗?

解决方案

我认为问题在于设置代表。



请将textfield委托给你的ViewController,如下所示

  class ViewController:UIViewController,UITextFieldDelegate {

然后为您的文本字段创建IBOutlet

  @IBOutlet var txtTest:UITextField = nil 

确保它连接到故事板视图中的文本字段。



最后使用



<$ p $设置其代理p> txtTest.delegate = self

我们差不多完成了。现在将此委托函数放入您的类中。

  func textFieldShouldReturn(textField:UITextField!) - > Bool //按下'return'键时调用。返回NO忽略。 
{
textField.resignFirstResponder()
返回true;
}

希望这能解决您的问题。


I have little experience in Objective-C. I want to hide the keyboard for a text field using the Swift programming language.

I also tried this

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
    return true;
}

But the method is not getting triggered when I hit return. Anyone have any luck with this?

解决方案

I think the Problem is with setting the Delegate.

Please set textfield Delegate to your ViewController like this

class ViewController: UIViewController,UITextFieldDelegate {

and then create the IBOutlet for your text field like this

@IBOutlet var txtTest : UITextField = nil

make sure that it is connected to your text field on your view in storyboard.

finally set its Delegate using

txtTest.delegate=self

We are almost done. Now place this delegate function into your class.

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
    {
        textField.resignFirstResponder()
        return true;
    }

Hope this will solve your issue.

这篇关于用swift编程语言隐藏文本字段的键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆