如何使我在 UIAlertController 内的 UITextField 中输入的数据在 Swift 中通过重置保持不变?
How to make data I enter in a UITextField inside a UIAlertController persist through resets in Swift?
我一直在尝试在我的浏览器应用程序中集成 Pinboard 书签视图(通过解析 RSS Feed 并将其显示在 TableView 中)。为了获取提要的用户名和 API 令牌,我在我的应用程序的设置视图中有一个 UIAlertController。输入的详细信息会在会话中保留,但如果我从多任务视图中强制退出应用程序,详细信息将被删除。我怎样才能让他们留下来?
这是我用于 UIAlertController 的代码:
@IBAction func pinboardUserDetailsRequestAlert(sender: AnyObject) {
//Create the AlertController
var pinboardUsernameField :UITextField?
var pinboardAPITokenField :UITextField?
let pinboardUserDetailsSheetController: UIAlertController = UIAlertController(title: "Pinboard Details", message: "Please enter your Pinboard Username and API Token to access your bookmarks", preferredStyle: .Alert)
//Add a text field
pinboardUserDetailsSheetController.addTextFieldWithConfigurationHandler({(usernameField: UITextField!) in
usernameField.placeholder = "Username"
var parent = self.presentingViewController as! ViewController
usernameField.text = parent.pinboardUsername
pinboardUsernameField = usernameField
})
pinboardUserDetailsSheetController.addTextFieldWithConfigurationHandler({(apiTokenField: UITextField!) in
apiTokenField.placeholder = "API Token"
var parent = self.presentingViewController as! ViewController
apiTokenField.text = parent.pinboardAPIToken
pinboardAPITokenField = apiTokenField
})
pinboardUserDetailsSheetController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
pinboardUserDetailsSheetController.addAction(UIAlertAction(title: "Done", style: .Default, handler: { (action) -> Void in
// Now do whatever you want with inputTextField (remember to unwrap the optional)
var parent = self.presentingViewController as! ViewController
parent.pinboardAPIToken = pinboardAPITokenField?.text
parent.pinboardUsername = pinboardUsernameField?.text
}))
//Present the AlertController
self.presentViewController(pinboardUserDetailsSheetController, animated: true, completion: nil)
}
Portland Runner 在问题的评论中已经回答了这个问题。有效的解决方案是使用 NSUserDefaults 保存文本。
感谢波特兰赛跑者! :)
我一直在尝试在我的浏览器应用程序中集成 Pinboard 书签视图(通过解析 RSS Feed 并将其显示在 TableView 中)。为了获取提要的用户名和 API 令牌,我在我的应用程序的设置视图中有一个 UIAlertController。输入的详细信息会在会话中保留,但如果我从多任务视图中强制退出应用程序,详细信息将被删除。我怎样才能让他们留下来?
这是我用于 UIAlertController 的代码:
@IBAction func pinboardUserDetailsRequestAlert(sender: AnyObject) {
//Create the AlertController
var pinboardUsernameField :UITextField?
var pinboardAPITokenField :UITextField?
let pinboardUserDetailsSheetController: UIAlertController = UIAlertController(title: "Pinboard Details", message: "Please enter your Pinboard Username and API Token to access your bookmarks", preferredStyle: .Alert)
//Add a text field
pinboardUserDetailsSheetController.addTextFieldWithConfigurationHandler({(usernameField: UITextField!) in
usernameField.placeholder = "Username"
var parent = self.presentingViewController as! ViewController
usernameField.text = parent.pinboardUsername
pinboardUsernameField = usernameField
})
pinboardUserDetailsSheetController.addTextFieldWithConfigurationHandler({(apiTokenField: UITextField!) in
apiTokenField.placeholder = "API Token"
var parent = self.presentingViewController as! ViewController
apiTokenField.text = parent.pinboardAPIToken
pinboardAPITokenField = apiTokenField
})
pinboardUserDetailsSheetController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
pinboardUserDetailsSheetController.addAction(UIAlertAction(title: "Done", style: .Default, handler: { (action) -> Void in
// Now do whatever you want with inputTextField (remember to unwrap the optional)
var parent = self.presentingViewController as! ViewController
parent.pinboardAPIToken = pinboardAPITokenField?.text
parent.pinboardUsername = pinboardUsernameField?.text
}))
//Present the AlertController
self.presentViewController(pinboardUserDetailsSheetController, animated: true, completion: nil)
}
Portland Runner 在问题的评论中已经回答了这个问题。有效的解决方案是使用 NSUserDefaults 保存文本。
感谢波特兰赛跑者! :)