2015-04-22 8 views
15

In einer meiner viewController möchte ich eine alert box erscheinen, die die user zur Eingabe dieser Informationen auffordert.Dann möchte ich, dass der Benutzer diese Eingabe mit speichern NSUserDefaults. Wie kann ich das erreichen?Swift: Einfügen von Alarmbox mit Texteingabe (und Speichern von Texteingabe)

Vielen Dank im Voraus!

+1

Bitte einige Details über das, was Sie bereits versucht haben, und was speziell ellute dich. Dies ist kein geeigneter Ort, um Ihre Forschung zu beginnen. Versuchen Sie zuerst [Google] (http://www.google.com). Kommen Sie hierher, wenn Sie etwas recherchiert haben und Hilfe zu einem bestimmten Punkt benötigen. –

Antwort

33

Check this out:

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) 
19

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) 
} 
2

In 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)