2014-10-10 54 views
0

我正在Swift中开发一个自定义键盘,我想知道键盘类型何时改变(例如数字或十进制键盘类型)。我相信这应该在textDidChangetextWillChange中完成。该方法收到UITextInput,其中有一个属性keyboardType。但是,当调用此方法时,该属性似乎始终为nil,因为即使在输入了不同的输入类型(数字)后,它也从不运行以下代码。为什么在textDidChange中textInput.keyboardType为零?

override func textDidChange(textInput: UITextInput) { 
    if let inputType = textInput.keyboardType { 
     //never gets here 
     deleteKeyboardButton.backgroundColor = UIColor.yellowColor() 
    } 
} 

回答

0

我发现你必须得到UITextDocumentProxy并使用其keyboardType属性,而不是textInput的财产。

var proxy = self.textDocumentProxy as UITextDocumentProxy 
if proxy.keyboardType == UIKeyboardType.EmailAddress { 
    //add code here to display email input keyboard 
}