以下代码表示如果userNameTF或passwordTF已满或为空,它将显示警报。当UITextField已满或为空时显示警报Swift
@IBAction func LoginBtn(sender: AnyObject) {
let userName = userNameTF.text
let password = passwordTF.text
if ((userName?.isEmpty) != nil) {
displayMyAlertMessage ("Forget to fill your user name")
return
}
if ((password?.isEmpty) != nil){
displayMyAlertMessage ("Forget to fill your password")
return
}
}
func displayMyAlertMessage(userMessage:String){
let myAlert = UIAlertController(title: "WOF", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
}
当检查可选布尔你需要处理一个第三可能的结果是零所以加一个很好的例子==真,假== ==或零 –
看看这个答案以及http://stackoverflow.com/questions/29381994/swift-check-string-for-nil-empty – user3353890
嗨@Roberto C Dl Garza:请检查我的答案并接受它。 –