-1

I have seen similar questions asked on this site and I believe I have followed what was advised, however I can't seem to have it working properly. Here is the sample of the first controller .m

  @interface FirstController : UITableViewController <UITextFieldDelegate,    UITableViewDataSource, UITableViewDelegate>
 {
     NSMutableDictionary * routesDictionary;
 }

 @property (nonatomic, strong) NSMutableDictionary * routesDictionary;

And the .h

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{ 
    if ([[segue identifier] isEqualToString:@"SelectRouteSegue"])       
    {
        SelectRouteController * selectRoutes = [[SelectRouteController alloc]init];   
        selectRoutes.diction = self.routesDictionary;
        UINavigationController *navController= [segue destinationViewController];            
        NSLog(@"my diction:%@", selectRoutes.diction);   
        //At this point I see that the values are indeed in selectRoutes.diction
        navController = [[UINavigationController alloc] initWithRootViewController:selectRoutes];        
    }    
}

And in the second controller .h

@interface SecondController : UITableViewController
   {
        NSMutableDictionary * diction;

   }
@property (nonatomic, strong) NSMutableDictionary * diction;

The second controller .m

@synthesize diction;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Select";

    NSLog(@"New Data: %@",diction);
    //I constantly get null values.
    routesArray = [diction objectForKey:@"routes"];
}

What is the cause of the null value of my NSMuableDictionary diction in the second controller?

DanKiiing
  • 102
  • 1
  • 11
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Nov 16 '14 at 15:39
  • @HotLicks I mentioned that in the first line of the question, I followed what was advised in the question you suggested but I haven't got it working – DanKiiing Nov 16 '14 at 15:45

1 Answers1

0

Your prepareForSegue is wrong in a couple of ways.

When that method is called, the new view controller has already been created. It is the segue's "destinationViewController". You should not alloc/init a new instance of SelectRouteController. Instead, you should ask the segue for a pointer to the destinationViewController and cast it to the proper type:

SelectRouteController * selectRoutes = 
  (SelectRouteController *) segue. destinationViewController;

The same goes for the navigation controller. Assuming your segue is a push segue, the segue mechanism will take care of pushing the new view controller on to the navigation controller's stack. You should not be creating a new view controller as you are in this line:

    navController = [[UINavigationController alloc] initWithRootViewController:selectRoutes];        

Unless I misunderstand, and your segue is some other type of segue, and you're linking to a new navigation controller.

If that's the case then you need to explain the structure of your storyboard and how the segues link up.

In any case your prepareForSegue method is messed up.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • thanks for your response, I changed my code to ' SelectRouteController * selectRoutes = [[SelectRouteController alloc]init]; selectRoutes.routesDictionary = self.routesDictionary; UINavigationController *navController= [segue destinationViewController]; NSLog(@"my diction:%@", selectRoutes.routesDictionary); ' The segue is in this format: FirstViewController to UINavigationController to SecondViewController – DanKiiing Nov 16 '14 at 16:16
  • 1
    @DanKiiing, you shouldn't be alloc init'ing anything! The navigation controller creates its root view controller for you, so you should be getting a pointer to it with the navigation controller's property, topViewController. – rdelmar Nov 16 '14 at 16:33
  • Note what I (and Dan) said about **NOT** allocating a SelectRouteController. Post an explanation of how your storyboard is structured, as an edit to your original post. What does the FirstController look like? Is it a navigation controller, or a view controller inside a navigation controller? What kind of segue is triggering the prepareForSegue method? A push segue? A modal segue? Something else? – Duncan C Nov 16 '14 at 21:34