简体   繁体   English

如何使我在UIAlertController内的UITextField中输入的数据通过Swift中的重置保持不变?

[英]How to make data I enter in a UITextField inside a UIAlertController persist through resets in Swift?

I have been trying to integrate a Pinboard bookmarks view (by parsing an RSS Feed and displaying it in a TableView) in my browser app. 我一直在尝试在浏览器应用程序中集成Pinboard书签视图(通过解析RSS Feed并将其显示在TableView中)。 To get the username and API Token for the feed I have a UIAlertController in the Settings view of my app. 为了获取提要的用户名和API令牌,我在应用程序的“设置”视图中有一个UIAlertController。 The details entered are preserved through the session but if I force quit the app from the multitasking view, The details are deleted. 输入的详细信息将在整个会话中保留,但是如果我从多任务视图中强制退出应用程序,则将删除这些详细信息。 How can I make them stay? 我如何让他们留下来?

This is the code I'm using for the 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)

}

This question has been answered by Portland Runner in the comments to the question. Portland Runner在对该问题的评论中已经回答了这个问题。 The solution that worked was to save the text using NSUserDefaults. 有效的解决方案是使用NSUserDefaults保存文本。

Thanks Portland Runner! 感谢波特兰亚军! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM