单击UITextView或单击return / done时取消键盘?

时间:2015-05-15 03:01:08

标签: ios swift uitextview

我正在编写第一个应用程序的过程中,当我点击它时,我无法解雇数字小键盘。当我点击背景时它会解散,但当我尝试点击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.
    }


}

2 个答案:

答案 0 :(得分:0)

如果您触摸的UiTextView将“editable”属性设置为“YES”,键盘将不会被忽略,因为它将再次成为第一个响应者。如果您不需要编辑其中的文本,请将该属性设置为“NO”(或从Interface Builder取消选中)。

答案 1 :(得分:0)

我不确定每个人是否都能正确理解我的问题,但是,我确实找到了解决方案!我想要做的是当我点击不可编辑的文本视图或背景时停止编辑文本字段。感谢那些早先帮助过的人!这是解决方案:

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

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

}

func handleSingleTap(识别器:UITapGestureRecognizer){

    self.view.endEditing(true)

}