2015-04-22 8 views
15

In uno dei miei viewController, voglio fare apparire un alert box che richiede allo user di digitare queste informazioni. Quindi, voglio che l'utente memorizzi questo input usando NSUserDefaults. Come posso raggiungere questo obiettivo?Swift: Inserisci casella di avviso con input testo (e memorizza input testo)

Grazie in anticipo!

+1

Si prega di dare qualche dettaglio su ciò che avete già provato, e che cosa in particolare ti ellude. Questo non è un posto appropriato per iniziare la tua ricerca. Prova prima [Google] (http://www.google.com). Vieni qui quando hai fatto qualche ricerca e vuoi aiuto con un punto specifico. –

risposta

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

a 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) 
Problemi correlati