[swift]当单击 UITextView 或通过单击做回报/解雇键盘吗?

标签: UITextView ios Swift
发布时间: 2015/7/1 22:23:32
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我是在编程我第一个应用程序的过程中,遇到解雇数字小键盘,当点击它的时候。当我点击它遣散的背景但是当我试着点击 UITextView,它并不代表下课。此外,还有没有"做"或"返回"按钮数字键盘上的吗?请要深入,因为不完全知道如何的一切只是还没有工作!在此先感谢。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate{

    @IBOutlet weak var CalcButton: UIButton!
    @IBOutlet weak var DaysLabel: UITextView!
    @IBOutlet weak var RateLabel: UITextView!
    @IBOutlet weak var CowsLabel: UITextView!
    @IBOutlet weak var PoundsLabel: UITextView!

    @IBOutlet weak var PoundsTextfield: UITextField!
    @IBOutlet weak var CowsTextfield: UITextField!
    @IBOutlet weak var DaysTextfield: UITextField!
    @IBOutlet weak var RateTextfield: UITextField!

    @IBAction func NDHIALink(sender: AnyObject) {
       UIApplication.sharedApplication().openURL(NSURL(string: "http://www.dhia.org/")!)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.DaysLabel.layer.cornerRadius = 5
        self.RateLabel.layer.cornerRadius = 5
        self.CowsLabel.layer.cornerRadius = 5
        self.PoundsLabel.layer.cornerRadius = 5
        self.CalcButton.layer.cornerRadius = 5

        DaysTextfield.delegate = self
        PoundsTextfield.delegate = self
        RateTextfield.delegate = self
        CowsTextfield.delegate = self
        self.CowsLabel.delegate = self
        self.PoundsLabel.delegate = self
        self.RateLabel.delegate = self
        self.DaysLabel.delegate = self
        DaysTextfield.returnKeyType = UIReturnKeyType.Done


    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        CowsTextfield.resignFirstResponder()
        PoundsTextfield.resignFirstResponder()
        DaysTextfield.resignFirstResponder()
        RateTextfield.resignFirstResponder()
        CowsLabel.resignFirstResponder()
        PoundsLabel.resignFirstResponder()
        DaysLabel.resignFirstResponder()
        RateLabel.resignFirstResponder()    }


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


}

解决方法 1:

我不确定如果大家正确理解我的问题,然而,我没有找到一个解决方案!想做什么是站编辑文本字段,当我点击非可编辑的 textview 或背景。感谢那些帮助过早!这里是解决方案:

 override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
    tapRecognizer.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(tapRecognizer)

}

func handleSingleTap(recognizer: UITapGestureRecognizer) {

    self.view.endEditing(true)

}
官方微信
官方QQ群
31647020