swift – 用uiTextView关闭键盘

前端之家收集整理的这篇文章主要介绍了swift – 用uiTextView关闭键盘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我相信这不是那么困难,但是我无法找到关于如何使用textview而不是textfield的return / done键关闭键盘的信息。这是我迄今为止所尝试过的(与文本框一起使用)

非常感谢任何帮助!

//  PostTravelQuestion.swift

class PostTravelQuestion: UIViewController,UITextViewDelegate {

    @IBAction func closepostpage(sender: AnyObject) {
        dismissViewControllerAnimated(true,completion: nil)
    }


    @IBOutlet var postquestion: UITextView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        postquestion.delegate = self
    }

    self addDoneToolBarToKeyboard:self.textView


    /*func textViewShouldEndEditing(textView: UITextView) -> Bool {

        textView.resignFirstResponder()

        return true
    }*/

    /*override func touchesBegan(touches: NSSet,withEvent event: UIEvent) {
        postquestion.resignFirstResponder()
        self.view.endEditing(true)
    }*/



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func textViewShouldReturn(textView: UITextView!) -> Bool {
        self.view.endEditing(true);
        return true;
    }
}
这对我有用:
import UIKit

class ViewController: UIViewController,UITextViewDelegate {


    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.delegate = self
    }

    func textView(textView: UITextView,shouldChangeTextInRange range: NSRange,replacementText text: String) -> Bool {
        if(text == "\n") {
            textView.resignFirstResponder()
            return false
        }
        return true
    }

}
原文链接:https://www.f2er.com/swift/320485.html

猜你在找的Swift相关文章