我已将 UITextField 添加到 UIAlertController,但是shouldChangeCharactersInRange
不会被解雇。为什么?我设置了代表。
let alertController = UIAlertController(title: "", message: "xxx", preferredStyle: .Alert)
self.presentViewController(alertController, animated:true, completion:nil)
let textField = UITextField()
textField.delegate = self
alertController.addTextFieldWithConfigurationHandler(nil)
在同一个班级中,代表:
func textField(textField: UITextField!,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String!) -> Bool {
您为其设置委托的文本字段与添加到警报控制器的文本字段不同。基本上,您正在创建 UITextField 的新实例,但永远不会为其提供框架,或将其添加到视图层次结构中。同时,您正在使用addTextFieldWithConfigurationHandler()
将文本字段添加到警报控制器,但您从未为此文本字段设置委托。我相信这就是你想要的:
let alertController = UIAlertController(title: "", message: "xxx", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler {[weak self] (textField: UITextField!) in
textField.delegate = self
}
self.presentViewController(alertController, animated:true, completion:nil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)