按返回键如何在swift中隐藏键盘? [英] How to hide keyboard in swift on pressing return key?

查看:96
本文介绍了按返回键如何在swift中隐藏键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITextfied ,同时点击了textfied键盘,但是当我按下返回键时,键盘没有消失。我使用了以下代码。

I am using UITextfied while clicking on textfied keyboard appear but when i pressed the return key, keyboard is not disappearing.I used the following code.

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

resignfirstresponder方法无法正常运行。

the method resignfirstresponder is not getting in function.

推荐答案

您可以使用以下功能让应用程序关闭键盘

You can make the app dismiss the keyboard using the following function

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

以下是一个完整的示例,可以更好地说明:

Here is a full example to better illustrate that:

//
//  ViewController.swift
//
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var myTextField : UITextField

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.myTextField.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }
}

代码来源: http://www.snip2code .com / Snippet / 85930 / swift-delegate-sample

这篇关于按返回键如何在swift中隐藏键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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