Proper way to handle data persistence/flow with na

2019-08-11 07:53发布

问题:

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. 
}

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.

回答1:

There are lots of ways to handle this.

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.

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.



回答2:

There are a number of ways you can persist data from class to class, and they all have their pros and cons. Some of the most common are:

Key Value Observing

KVO is one of Apple's favorites, and can be very powerful. It allows objects to monitor changes to properties of other objects, and react appropriately when those changes occur. Lots of great tutorials out there, I've linked Apple's KVO guide and an NSHipster tutorial I found useful when I was starting.

Apple: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

NSHipster: http://nshipster.com/key-value-observing/

NSNotificationCenter

The NSNotificationCenter allows an object to broadcast notifications that can be listened for by any other object that cares to listen. It's less specific than KVO and can be used to transfer data when you don't know exactly where it's coming from or who needs it. This link compares KVO and NSNotificationCenter pretty nicely.

NSHipster: http://nshipster.com/nsnotification-and-nsnotificationcenter/

There are various other methods, including delegates and callbacks, but this will definitely get you started! Check out nshipster.com and of course Apple's documentation for tons great beginning resources as you come across concepts you don't know.