在我的一個viewController
中,我想讓一個alert box
出現,提示user
鍵入此信息。然後,我希望用戶存儲此輸入使用NSUserDefaults
。我怎樣才能做到這一點?Swift:插入帶有文本輸入的提示框(和存儲文本輸入)
預先感謝您!
在我的一個viewController
中,我想讓一個alert box
出現,提示user
鍵入此信息。然後,我希望用戶存儲此輸入使用NSUserDefaults
。我怎樣才能做到這一點?Swift:插入帶有文本輸入的提示框(和存儲文本輸入)
預先感謝您!
檢查了這一點:
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .Alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .Default) { (_) in
if let field = alertController.textFields![0] as? UITextField {
// store your data
NSUserDefaults.standardUserDefaults().setObject(field.text, forKey: "userEmail")
NSUserDefaults.standardUserDefaults().synchronize()
} else {
// user did not fill field
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in }
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Email"
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
SWIFT 3
func presentAlert() {
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let field = alertController.textFields?[0] {
// store your data
UserDefaults.standard.set(field.text, forKey: "userEmail")
UserDefaults.standard.synchronize()
} else {
// user did not fill field
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
在SWIFT 3
let alertController = UIAlertController(title: "SecureStyle", message: "SecureStyle AlertView.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField) -> Void in
textField.secureTextEntry = true
textField.placeholder = "Password"
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
print("Cancel")
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
print(alertController.textFields?.first?.text)
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)
請對你已經嘗試了一些細節,和什麼專門呃你。這不適合開展你的研究。首先嚐試[Google](http://www.google.com)。當你做了一些研究並且希望得到某一特定點的幫助時,請到這裏。 –