0

I am trying to pass data from one view controller to another.... I have a collection view in the first view controller which is populated dynamically with an array and want it so that when an item is clicked... it opens up the second view controller (which I've done) but sends along data (in this case an array)

Ive looked into core data, but after trying to implement it thought it might be a bit "overkill" for such a basic function? (or am i wrong?)

Anyone have any ideas?

Thanks, Dave

6
  • Have you tried using segues? Commented Sep 8, 2014 at 19:40
  • Assuming you are using segues then prepareForSegue is the best way to do this. You override this in your initial view controller and are passed in a UIStoryboardSegue which has a segue.destinationViewController which you can use to set any data on that controller Commented Sep 8, 2014 at 19:42
  • Just set a property on the target view controller. Commented Sep 8, 2014 at 19:44
  • Thanks for the replies guys! I am using: var itemViewController = mainStoryboard.instantiateViewControllerWithIdentifier("projectViewController") as UIViewController self.presentViewController(itemViewController, animated: true, completion: nil) is there anyway I can pass data similar to the segues? Hi @jeffamaphone, how would I set a property on the target controller from my initial view controller? Thanks, Dave Commented Sep 9, 2014 at 8:01
  • Ok so i just tried something.... I set a variable in my app delegate file and then in my other controllers accessed it like this: let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate); let context:NSManagedObjectContext = appDelegate.managedObjectContext!; println(appDelegate.test) it works...but is it ok to use the app delegate like this? Thanks Commented Sep 9, 2014 at 8:09

1 Answer 1

1

Have you tried using prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // pass data to next view
}

Or NSUserDefaults:

NSUserDefaults.standardUserDefaults().setObject(VALUE, forKey:YOUR_KEY)
NSUserDefaults.standardUserDefaults().synchronize()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply! didn't have much luck trying to implement the NSUserDefaults, any ideas on the comments I left above? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.