【发布时间】:2017-12-25 09:34:25
【问题描述】:
我有一个接受用户输入的 UIAlertController。我想将用户输入的文本添加到数组中,并将该数组保存在用户默认值中。问题是,新输入的文本不断替换以前输入的文本。所以我认为它没有保存在数组中。
这是我迄今为止尝试过的:
func presentAlert() {
let confirmAction = UIAlertAction(title: "Save", style: .default) { (_) in
if let field = alertController.textFields {
// This is how I tried to create the array to hold input text
let textFieldArray = field as [UITextField]
let text = textFieldArray[0].text
var myArray = [""]
myArray.append(String(describing: text!))
UserDefaults.standard.set(myArray, forKey: "Gratitude")
UserDefaults.standard.synchronize()
print(myArray)
} else {
print()
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
//textField.placeholder = ""
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
【问题讨论】:
-
你每次写一个字符串的数组。这取代了以前可能存在的任何东西。
标签: ios arrays swift uitextfield nsuserdefaults