Swift:插入带有文本输入的警报框(并存储文本输入)

Swift:插入带有文本输入的警报框(并存储文本输入),swift,uialertview,nsuserdefaults,Swift,Uialertview,Nsuserdefaults,在我的一个viewController中,我想显示一个警报框,提示用户键入此信息。然后,我希望用户使用NSUserDefaults存储此输入。我怎样才能做到这一点 提前谢谢你 看看这个: let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert) let confirmAction = UIAlertAction(tit

在我的一个
viewController
中,我想显示一个
警报框
,提示
用户
键入此信息。然后,我希望用户使用
NSUserDefaults
存储此输入。我怎样才能做到这一点

提前谢谢你

看看这个:

let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)

let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
  guard let textFields = alertController.textFields,
    textFields.count > 0 else {
      // Could not find textfield
      return
  }

  let field = textFields[0]
  // store your data
  UserDefaults.standard.set(field.text, forKey: "userEmail")
  UserDefaults.standard.synchronize()
}

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

func presentAlert() {
    let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)

    let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
        if let emailTextField = alertController.textFields?[0] {
            // do your stuff with emailTextField
        } 
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

    alertController.addTextField { (textField) in
        textField.placeholder = "Email"
    }

    alertController.addAction(confirmAction)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}
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)

在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 emailTextField = alertController.textFields?[0] {
            // do your stuff with emailTextField
        } 
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

    alertController.addTextField { (textField) in
        textField.placeholder = "Email"
    }

    alertController.addAction(confirmAction)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}
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)

@为什么不给他一个答案呢?你为什么要这样?下面有3个人给出了答案,而你正试图表现得聪明一点$$