我想防止保存数据并在任何文本字段为空时显示警报。这就是我所拥有的,当我点击保存按钮时,弹出警报,但是文本框在空白时仍然保留空白文本。检查UITextfield是否为空并显示警报
// check if text feild is empty
if usernameLabel.text == "" || passwordLabel.text == "" || phoneNumber.text == "" || service.text == "" || address.text == "" {
// Alert
let optionMenu = UIAlertController(title: nil, message: "Please Enter Text", preferredStyle: .Alert)
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Ok", style: .Cancel, handler:
nil)
optionMenu.addAction(cancelAction)
// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)
}
} //End save button
你写的,如果(usernameLabel .hasText()|| passwordLabel .hasText()){ } –
是攒动写在别的这种情况的? –
偏离主题,但我不会将“UITextField”实例命名为“用户名**标签**”或“密码**标签**”;如果只是为了避免混淆... –