Ios 如果UITextField不为空,请检查live
我有一个包含UIExtField的UIAlertView。警报视图有两个按钮。一个正常的“关闭”按钮和另一个“添加”。如果UITextField不为空,“添加”按钮应仅可单击。如果字段为空,我不知道如何“实时”签入。我只看到了在按钮被点击时检查文本字段的可能性。但这不是我们想要的(“添加”按钮应该是灰色的,如果xtfield是空的)。你有解决这个问题的办法吗?谢谢你的努力 我正在使用Swift 3和Xcode 8检查文本字段文本:Ios 如果UITextField不为空,请检查live,ios,swift,uitextfield,uialertview,xcode8,Ios,Swift,Uitextfield,Uialertview,Xcode8,我有一个包含UIExtField的UIAlertView。警报视图有两个按钮。一个正常的“关闭”按钮和另一个“添加”。如果UITextField不为空,“添加”按钮应仅可单击。如果字段为空,我不知道如何“实时”签入。我只看到了在按钮被点击时检查文本字段的可能性。但这不是我们想要的(“添加”按钮应该是灰色的,如果xtfield是空的)。你有解决这个问题的办法吗?谢谢你的努力 我正在使用Swift 3和Xcode 8检查文本字段文本: if textField.text != nil &&a
if textField.text != nil && textField.text != "" {
print("you can enable your add button")
}else{
print("empty text field")
}
检查文本字段文本:
if textField.text != nil && textField.text != "" {
print("you can enable your add button")
}else{
print("empty text field")
}
你可以授权。将当前视图控制器或视图设置为委托。如
let textTextField = UITextField()
textTextField.delegate = self
当textfield更改时,将调用下面的方法
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool
或者您可以使用通知UITextFieldTextDidChange。当textfield的文本发生更改时,将发布该通知
受影响的文本字段存储在通知的对象参数中。您可以使用TextFieldDelegate。将当前视图控制器或视图设置为委托。如
let textTextField = UITextField()
textTextField.delegate = self
当textfield更改时,将调用下面的方法
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool
或者您可以使用通知UITextFieldTextDidChange。当textfield的文本发生更改时,将发布该通知
受影响的文本字段存储在通知的对象参数中。您需要创建UIAlertAction作为全局变量,如下所示
var alertAction = UIAlertAction()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let userEnteredString = textField.text
let newString = (userEnteredString! as NSString).replacingCharacters(in: range, with: string) as NSString
if newString != ""{
alertAction.isEnabled = true
} else {
alertAction.isEnabled = false
}
return true
}
现在,在将该操作添加到UIAlertController时,需要将属性isEnabled
设置为false,如下代码所示
alertAction = UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)
alertAction.isEnabled = false
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField { (textField) in
textField.delegate = self
}
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
在此之后,在委托方法shouldChangeCharacter中,您需要获取该值。如果在UITextField中输入该值,则需要像下面这样启用该按钮
var alertAction = UIAlertAction()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let userEnteredString = textField.text
let newString = (userEnteredString! as NSString).replacingCharacters(in: range, with: string) as NSString
if newString != ""{
alertAction.isEnabled = true
} else {
alertAction.isEnabled = false
}
return true
}
下面是一个工作示例
您需要将UIAlertAction创建为如下全局变量
var alertAction = UIAlertAction()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let userEnteredString = textField.text
let newString = (userEnteredString! as NSString).replacingCharacters(in: range, with: string) as NSString
if newString != ""{
alertAction.isEnabled = true
} else {
alertAction.isEnabled = false
}
return true
}
现在,在将该操作添加到UIAlertController时,需要将属性isEnabled
设置为false,如下代码所示
alertAction = UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)
alertAction.isEnabled = false
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField { (textField) in
textField.delegate = self
}
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
在此之后,在委托方法shouldChangeCharacter中,您需要获取该值。如果在UITextField中输入该值,则需要像下面这样启用该按钮
var alertAction = UIAlertAction()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let userEnteredString = textField.text
let newString = (userEnteredString! as NSString).replacingCharacters(in: range, with: string) as NSString
if newString != ""{
alertAction.isEnabled = true
} else {
alertAction.isEnabled = false
}
return true
}
下面是一个工作示例
您需要使用UITextFieldDelegate来侦听更改。
UIAlertView
不久前被弃用。您应该使用UIAlertController
。您需要使用UIExtFieldDelegate来侦听更改。UIAlertView
不久前被弃用。您应该使用UIAlertController
。您知道如何使其成为一个实用程序类,从而不必在每个要使用该类的viewcontroller中实现委托模式吗?我对它有一些问题()您知道如何使它成为一个实用程序类,这样您就不必在每个要使用它的viewcontroller中实现委托模式了吗?我有一些问题()任务不是问这样的问题,是的,这可以检查文本字段是否为空。任务不是问这样的问题,是的,这可以检查文本字段是否为空。