2017-08-02 106 views
0

我有兩個UITextFields和數字鍵盤UIButton在同一個窗體上。我不希望用戶使用默認鍵盤,但使用我的UIButton將值輸入到它。但每次點擊textField時,都會顯示默認鍵盤。Swift 3禁用或隱藏鍵盤UITextField

我試圖將isUserInteractionEnabled設置爲false,鍵盤沒有顯示,但動作選擇器功能沒有觸發touchdown事件,這就是爲什麼我無法更改當前UITextField背景顏色。

請幫忙!!!

+0

isUserInteractionEnabled是假的的UITextField和觸下承擔的UIButton我是rightt? –

+0

我希望touchdown事件觸發UITextField上的選擇器函數,但是當isUserInteractionEnabled爲false時,觸發未觸發 –

+0

您應該啓用isUserInteractionEnabled,然後纔可以輕擊工作。在觸摸下請添加self.view.endEditing(true) –

回答

0

爲UITextField isUserInteractionEnabled真實,讓

settings.addTarget(self, action: #selector(touched), for: .touchUp) 

func touched(sender: UIButton!) { 
    self.view.endEditing(true) 
    // change background here 
    } 

您也可以嘗試resignFirstResponder()隱藏鍵盤。

+0

我無法更改背景,但如果我從TouchUpInside更改爲TouchDown,則會執行endEditing(true)下的代碼,但鍵盤仍顯示 –

0

試試這個代碼斯威夫特3.

import UIKit 


class ViewController: UIViewController, UITextFieldDelegate { 

    @IBOutlet weak var myTextField: UITextField! 


    override func viewDidLoad() 
    { 
    self.myTextField.delegate = self 
    } 

    @IBAction func Button2Pressed(_ sender: Any) 
    { 
    self.myTextField.text = "one" 
    } 

    @IBAction func Button1pressed(_ sender: Any) 
    { 
    self.myTextField.text = "two" 
    } 

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 
    { 
    return false 
    } 

    override func didReceiveMemoryWarning() 
    { 
    super.didReceiveMemoryWarning() 
    } 
}