使用导航控制器处理数据持久性/流的正确方法? [英] Proper way to handle data persistence/flow with navigation controllers?

查看:21
本文介绍了使用导航控制器处理数据持久性/流的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果我想将信息发送到我要导航到的视图控制器,我只需在 prepareForSegue 函数中指定它,如下所示:

Normally if I want to send information to a view controller I'm navigating to, I'll just specify it in the prepareForSegue function like so:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
   let vc = segue.destinationViewController as! SomeViewController
   vc.info = self.info
 }

但据我所知,这仅在我创建要导航到的视图控制器的新实例时才有效.

but as far as I understand it, this only works when I am creating a new instance of the view controller that is being navigated to.

假设我在导航栏中有一个按钮,它有一个(显示)转场到不同的视图控制器,我想在弹出它或按下后退按钮后将我在新视图控制器中的数据持久化.这样做的最佳做法是什么?我不确定如何创建对父视图控制器的引用.现在我只是将我需要的信息存储在全局变量中,但如果感觉非常错误.

Say I have a button in a navigation bar that has a (show) segue to a different view controller and I want to persist the data I have in the new view controller after I pop it or push the back button. What is the best practice for doing this? I'm not sure how to create a reference to the parent view controller. Right now I'm just storing the info I need in global variables, but if feels very wrong.

我正在做什么的快速示例:

Quick example of what I'm doing:

var globalInfo:Int? 
class FirstViewController: UIViewController {

   //this view controller is connected to SecondViewController via a navigation
   //item that I created in the UI builder. It is a "show" segue

   //other code
}

两个控制器通过故事板连接

the two controllers are connected via storyboard

class SecondViewController: UIViewController {
    //code
    //code
    func doWork {
       globalInfo = 0 // this is the global variable specified in the first view controller
    }
   //so now when I push back, globalInfo will still be set to 0. 
}

我知道这不是管理数据流的好方法,但我是 iOS 新手,我不知道这种情况下的最佳做法.如果我问一个重复的问题,请原谅我.我不太了解术语,所以我不确定要搜索什么.

I know that this isn't a good way to manage data flow, but I'm new to iOS and I don't know the best practices in this situation. Forgive me if I am asking a repeated question. I don't know the terminology very well, so I'm not sure what to search for.

推荐答案

有很多方法可以解决这个问题.

There are lots of ways to handle this.

如果你有一个导航控制器并且你正在做一个显示转场,那么当你完成时弹出,并且你正在将信息从源传递到目的地(就像在主/细节设计中一样)那么它确实使在 prepareForSegue 中传递信息的意义.

If you have a navigation controller and you are doing a show segue, then a pop when you're done, and you are passing information from the source to the destination (like in a master/detail design) then it does make sense to pass the info in prepareForSegue.

您可以通过引用传递数据对象,并让源视图控制器保留对该数据对象的引用.然后当源视图控制器的 viewWillAppear 方法触发时,您可以检查数据对象的变化并采取相应的行动.

You can pass a data object by reference and have the source view controller keep a reference to that data object. Then when the source view controllers' viewWillAppear method fires, you can check the data object for changes and act accordingly.

您还可以使用委托属性设置目标视图控制器,并使源视图控制器成为目标的委托.定义目标视图控制器用来与其委托进行通信的协议.

You can also set up the destination view controller with a delegate property, and make the source view controller the delegate of the destination. Define a prototocol that the destination view controller uses to communicate with it's delegate.

然后在目标视图控制器中,您可以向委托发送消息以告诉它用户所做的更改.

Then in the destination view controller you can send messages to the delegate to tell it about changes the user makes.

如果您处理的数据对您的应用具有全局意义,那么使用某种应用范围的共享数据存储是合理的.在这种情况下,我可能会使用数据容器单例.

If the data you are dealing with has global meaning to your app then using some sort of app-wide shared data store is reasonable. I would likely use a data container singleton in that case.

哪种方法最好取决于您的视图控制器正在做什么以及数据在您的应用设计中意味着什么.

What approach is best really depends on what your view controllers are doing and what the data means in your app design.

这篇关于使用导航控制器处理数据持久性/流的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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