2017-05-08 78 views
-3
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(try.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) 
+0

在此添加一些文本。問題的標題並不意味着除代碼之外的唯一文本。 – Aziuth

+0

問題是什麼? – JeremyP

回答

1

錯字:

UserDefaults.standard.set(field.text, forKey: "userEmail") 

而不是

UserDefaults.standard.set(try.text, forKey: "userEmail") 

錯誤消息指出它:

... value of type '(_) throws ->()'

+0

這是我認爲的正確答案。 'try'使它看起來像編譯器正在拋出一樣。 – JeremyP

-1

使用這個....

let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (action) -> Void in 
     if let field = alertController.textFields?[0] { 
      // store your data 
      UserDefaults.standard.set(try.text, forKey: "userEmail") 
      UserDefaults.standard.synchronize() 
     } else { 
      // user did not fill field 
     } 
    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in 
    alertController.addTextField { (textField) in 
     textField.placeholder = "Email" 
    }) 
相關問題