如何使我在 UIAlertController 内的 UITextField 中输入的数据通过 Swift 中的重置而持久化? [英] How to make data I enter in a UITextField inside a UIAlertController persist through resets in Swift?

查看:11
本文介绍了如何使我在 UIAlertController 内的 UITextField 中输入的数据通过 Swift 中的重置而持久化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的浏览器应用程序中集成 Pinboard 书签视图(通过解析 RSS 源并将其显示在 TableView 中).要获取提要的用户名和 API 令牌,我在应用程序的设置"视图中有一个 UIAlertController.输入的详细信息通过会话保留,但如果我从多任务视图强制退出应用程序,详细信息将被删除.我怎样才能让他们留下来?

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. To get the username and API Token for the feed I have a UIAlertController in the Settings view of my app. 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?

这是我用于 UIAlertController 的代码:

This is the code I'm using for the 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 保存文本.

This question has been answered by Portland Runner in the comments to the question. The solution that worked was to save the text using NSUserDefaults.

感谢波特兰赛跑者!:)

Thanks Portland Runner! :)

这篇关于如何使我在 UIAlertController 内的 UITextField 中输入的数据通过 Swift 中的重置而持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆