以下代碼表示如果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:請檢查我的答案並接受它。 –