持久化数据并传递给多个视图 [英] Persist data and pass to multiple views

查看:24
本文介绍了持久化数据并传递给多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户通过输入他们的详细信息登录,并向我的 API 发送 HTTP GET 请求,该请求对用户进行身份验证并将用户数据/用户对象从数据库发回.

完成后,在点击按钮时触发的 HTTP 请求中,我将用户数据发送到下一个视图,并使用以下代码执行到视图的转换:

 如果让 parseJSON = json{//parseJSON 包含返回数据.我们正在以编程方式在两个视图之间创建一个 segue//在它们之间传递数据.dispatch_async(dispatch_get_main_queue()){让 storyboard = UIStoryboard(name: "Main", bundle: nil)让 vc = storyboard.instantiateViewControllerWithIdentifier("mainView") 作为!主视图控制器var fullname = parseJSON["employeeName"] as!细绳var job = parseJSON["jobTitle"] as!细绳var email = parseJSON["email"] as!细绳vc.username = 全名vc.jobTitle = 工作vc.userEmail = 电子邮件self.presentViewController(vc,动画:true,完成:nil)}

上述工作完美.从我的第二个视图中,我为另一个视图创建了一个准备转场,我试图在用户使用以下代码登录时传递刚刚传递给第二个视图的用户数据:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {如果(segue.identifier ==设置"){//将数据传递给下一个视图让 viewController = segue.destinationViewController as!设置表视图控制器viewController.username = 用户名viewController.jobTitle = jobTitle}}

这再次正常工作.当我点击按钮转到视图 3 时,prepareforsegue 函数传递数据并显示以下视图(忽略电子邮件字段):

但是当我点击后退按钮并尝试访问同一个视图时所有来自 API 的数据以及从视图 1 到视图 2 到视图 3 的所有数据都消失了.见下面:

我确实理解发生这种情况的原因,我正在向 1 个方向传递数据,并且它没有被保存,只是从一个视图传递到另一个视图,当我返回一个视图时,在中断之间传递的过程以及数据丢失了.

我的问题是,当用户首次登录并且 API 将数据发回时,如何保留数据.我可以保存吗?并继续通过多个视图传递它,无论如何仍然保留它?

任何帮助将不胜感激.提前致谢.

解决方案

简而言之:将您获得的数据存储在视图中的实例变量中,并将该变量用于转场.

详细解释:最佳实践是为您从 API 获取的内容创建一个模型类,在检索信息时将所有数据放入该类的变量中,就像在视图类中具有该类型的变量一样.

提示:阅读一些关于 MVC 范式的内容(网上有很多东西,如果你有时间阅读四人组的设计模式一书)

I have an app where a user logs in by entering their details and that sends a HTTP GET request to my API which authenticates the user and sends the users data/user object back from the database.

Once this is done, within my HTTP request which is triggered on button tap I send the users data onto the next view and perform a transition to the view by using the following code:

            if let parseJSON = json
            {
                // parseJSON contains the return data. We are programmatically creating a segue between two views
               //  passing data between them.
                dispatch_async(dispatch_get_main_queue())
                    {

                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let vc = storyboard.instantiateViewControllerWithIdentifier("mainView") as! MainViewController
                    var fullname = parseJSON["employeeName"] as! String
                    var job = parseJSON["jobTitle"] as! String
                    var email = parseJSON["email"] as! String
                    vc.username = fullname
                    vc.jobTitle = job
                    vc.userEmail = email
                    self.presentViewController(vc, animated: true, completion: nil)

            }

The above works perfectly.The from my second view I create a prepare for segue for another view where I am trying to pass the user data that was just passed to the 2nd view when the user logged in by using the code below:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "settings") {
        // pass data to next view
        let viewController = segue.destinationViewController as! SettingsTableViewController
        viewController.username = username
        viewController.jobTitle = jobTitle
    }
}

This again works properly. When I tap on the button to go to view 3 the prepareforsegue function passes the data and shows the following view (ignore email field):

But when I click on the back button and try to access to the same view all the data thats coming from the API and is being passed from View 1 to View 2 to View 3 disappears. See below:

I DO understand the reason why this happening, I am passing data in 1 direction and it is not being saved only being passed from one view to another and when I go back a view that process of passing between breaks and hence the data is lost.

My question is how can I preserve data when the user first logs in and the API sends the data back. Can I save it? and keep passing it through multiple views and still keep it no matter what?

Any help will be greatly appreciated. Thanks in advance.

解决方案

In short : Store the data you get in an instance variable in your view and use that variable for the segues.

Long Explanation: Best practice is to create a model class for the stuff you're getting from your API, put all data in a variable of that class when retrieving the info, and like a said have a variable of that type in your view classes.

Tip: read a bit about the MVC paradigma's (lotsa stuff online, if you have some time read the book Design Patterns by the gang of four)

这篇关于持久化数据并传递给多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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