0

I'm trying to figure out how to navigate around my app. But i'm a little lost.

I have a UIViewController that loads some data, then displays the data in a CollectionView. Then I have another UIViewController for the detailed view. I then trigger a segue to go to it, I pass the data etc.

self.performSegueWithIdentifier("detailViewSeque", sender: nil)

But the part i'm lost on is getting back to my main view, if I just trigger another segue then it loads all the data / view again. The data has already been loaded once, I really don't want to keep loading it.

I feel like I'm doing things wrong, that theres some super obvious way to handle this scenario.

Could someone point me in the right direction?

Scott
  • 309
  • 2
  • 13
  • When you say "load" do you mean pull from the network or a database? If so then you should keep the data around in your first UIViewController as a variable. If by "lead" you mean create a UIViewController from a storyboard that is a tiny cost that doesn't delay your app at all – Kevin May 15 '15 at 00:23
  • @Kevin I'm loading JSON from a server, it takes around 1.3 - 1.5 seconds. so it's quite a large cost unfortunately. – Scott May 15 '15 at 00:26

2 Answers2

3

This is good situation to use an unwind segue (for more information: What are Unwind segues for and how do you use them?). Here's how to setup one up:

Firstly, create an @IBAction in the view controller you want to segue to, that takes a UIStoryboardSegue as its only argument. For example:

@IBAction func unwindToHere(segue: UIStoryboardSegue) {
    // If you need you have access to the previous view controller 
    // through the segue object.
}

Secondly, you need to create the unwind segue in IB. To do this ctrl-drag from the view controller you want to segue from, to Exit and select the unwindToHere method:

enter image description here

Thirdly, you need to give your segue and identifier. To do this select your segue (see below - your segue will not be visible like normal segues); then use the Attribute Editor to give your segue an identifier.

enter image description here

Now you can use your segue. On the view controller you want to segue from, call:

self.performSegueWithIdentifier("YourID", sender: self)
Community
  • 1
  • 1
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
1

To rephrase your needs "I have data that I need to keep around somewhere that isn't associated with a view controller".

You have a few options here. Your goal is basically to store it somewhere that isn't going to go out of memory. The AppDelegate gets used for this purpose a lot but Singleton variable works as well.

I would personally create a singleton, say CatPictureRetriever with

private let _CatPictureRetriever SharedInstance = CatPictureRetriever()

class CatPictureRetriever {
    static let sharedInstance = CatPictureRetriever()

    var catPictures : NSArray?;

    func gimmeCatPictures -> NSArray? {
        return catPictures
    }
}

Now you can get your pictures though your CatPictureRetriever anywhere

var pictures = CatPictureRetriever.sharedInstance.gimmeCatPictures()
Kevin
  • 16,696
  • 7
  • 51
  • 68