如何从UIAlertController保存文本

时间:2016-08-15 16:52:45

标签: swift

我想保存按下保存按钮时要保存的文本中输入内容的数据。这是我的代码:

@IBAction func buttonTappedToSetAReminder(sender: AnyObject) {

     let alertController = UIAlertController(title: "Reminders", message: "", preferredStyle: UIAlertControllerStyle.Alert)

     let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: {
          alert -> Void in

          _ = alertController.textFields![0] as UITextField

     })

     let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
          (action : UIAlertAction!) -> Void in

     })

     alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
          textField.placeholder = "Put a reminder!!!"
     }

     alertController.addAction(saveAction)
     alertController.addAction(cancelAction)

     self.presentViewController(alertController, animated: true, completion: nil)

}

2 个答案:

答案 0 :(得分:0)

let textField = alertController.textFields![0] as UITextField
// do something with textField.text

答案 1 :(得分:0)

似乎NSUserDefaults / UserDefaults是一个选项:

斯威夫特2:

let textField = alertController.textFields![0]
NSUserDefaults.standardUserDefaults().setObject(textField.text, forKey: "text")

斯威夫特3:

let textField = alertController.textFields![0]
UserDefaults.standard.set(textField.text, forKey("text")