0

I've only been doing objective c for 2 days so if this is a horrible question I apologise. So essentially I'm using storyboard to make an iPhone app. On one of my view's cells are dynamically created based on retrieving what to display in json format via a http request. When one of these cells is selected a view needs to appear, the same view for each cell (not a question about Segue's). This view will rely on which of the cells is selected (and not only that, but other info from the json that isn't displayed to the user). Hence, I need to pass a NSDictionary to the child view. I'm sure this is trivial yet I have found no solution. Thanks in advance for any help.

user1170665
  • 45
  • 1
  • 4

2 Answers2

0

Just like Tim said, you declare a property for your dictionary object then in the parent VC's code file (.m file), you:

#import "ChildViewController.h" // Replace that with the name of the class for your child VC

and do this in the prepareForSeque method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure we are dealing with the proper Segue
    if ([segue.identifier isEqualToString:@"mySegueID"]) // ALWAYS use identifiers for your Segues
    {
        // Setup the destination ViewController
        ChildViewController *cvc = segue.destinationViewController;


        // Set the dictionary object property on the ChildViewController
        cvc.myDict = TheDictionaryObjectYouWantToPass;
    }
}

This is how you pass objects using storyboards. The method that will get called when you have a Segue is this prepareForSegue method. This is where you pass objects, and do anything related to setting up the destination view controller

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
-1

You would use properties. Properties are externally exposed values of a class. They can be set or read. When you define a property, Xcode synthesizes setters and getters - that is, it automatically creates the -(type)variable and setVariable:(type) methods on the class, using the strategies provided: retain meaning the class will retain it - as opposed to assign where the class doesn't hang onto it, and nonatomic means that it will update it in place instead of copying the new value to another part of memory and updating the reference only after it is done. nonatomic is typically used in applications where thread safety is less of a concern, as it is faster than atomic, but it is not thread-safe.

In your child view's header:

@property (nonatomic, retain) NSDictionary* myDict;

In your parent view's method which presents the child view

// ... code which allocs child view here...
[childView setMyDict:theDict];
// ... code which presents child view here...
Tim
  • 14,447
  • 6
  • 40
  • 63
  • Because I am using storyboards there is no code that allocates the child view? Well code that I am editing. – user1170665 Jan 26 '12 at 07:36
  • Dear Tim, w.r.t. your answer segment -- *and nonatomic is some baloney nobody where knows how it works but it has to do with thread lock synchronization. -- * This has everything to do with thread synchronisation . Refer to the link:http://webcache.googleusercontent.com/search?q=cache:QhnQmTk3rQIJ:http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties%2Bdeveloper.apple+:+iphone+nonatomic&oe=utf-8&rls=org.mozilla%3Aen-US%3Aofficial&client=firefox-a&gs_l=heirloom-serp.3...5527.10398.0.10658.30.26.0.1.0.4.315.3136.11j7j4j3.25.0...0.0...1ac.1.iYOL3a6g7To&hl=en&ct=clnk – Abhishek Bedi Apr 08 '13 at 09:04
  • Haha - embarrassingly old answer. Will update with actual info about nonatomic. – Tim Apr 08 '13 at 19:36