7

In my app, I use a storyboard and segues, and I often pass data to the destination view controller before doing the segue, as follows:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
        [segue.destinationViewController performSelector:@selector(setMyData:) 
                                          withObject:myData];
    }
} 

It works everywhere except in one place. The selector gets called, the data gets set, but when the segue completes and the destination controller appears, it doesn't have the data I just set. After printing the view controller's id in both the source and destination view controllers, I found that the segue.destinationViewController in the code above is a DIFFERENT instance of the view controller than the one that gets displayed. What's going on here?

[UPDATE 1] I looked into the lifecycle of the destination view controller, and it first gets loaded during the segue execution, but AFTER I set the property on it! This means, that when I call performSelector on it, the view controller object is not initialized! This is why the data I set doesn't stick. t don't understand why is this the case, and why this same approach works in the other parts of my app.

[UPDATE 2] Posting the code of setMyData by request. At first I didn't have this method at all, because locationToOpen is a public property. I only added it to ensure it gets called and to print the debug info.

- (void)setMyData:(MyData *)myData
{
    DLog(@"self: %@", (id)self);
    _myData = myData;
}
22
  • Is the destinationViewController a UINavigationController?
    – adambinsz
    Jun 2, 2012 at 1:49
  • @adambinsz: it's a UIViewController that's embedded in a UINavigationController. The segue in the IB goes straight to the UIViewController. Jun 2, 2012 at 1:52
  • Are you sure it's not going to the navigation controller? Try using [[segue.destinationViewController viewControllers] objectAtIndex:0]
    – adambinsz
    Jun 2, 2012 at 1:55
  • Do you mean [[ ( (UIViewController *)segue.destinationViewController).navigationController viewControllers] objectAtIndex:0] ? It returns null. BTW, I don't think it's even possible to segue to a UINavigationController. I get a runtime error when I do that. Jun 2, 2012 at 2:07
  • 2
    Yes, it is possible to segue to a UINavigationController with a UIViewController embedded within it. I had a similar problem and replaced segue.destinationViewController with [[[segue destinationViewController] viewControllers] objectAtIndex:0] and it worked fine.
    – adambinsz
    Jun 2, 2012 at 2:12

4 Answers 4

5

I would do it as follows -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"NextSegue"]) 
    {
        DestinationViewController *dest = [segue destinationViewController];

        dest.myData = self.myData; 
    }
}

And in the DestinationViewController.h, you should create a property -

#import <UIKit/UIKit.h>

@interface DestinationViewController : UIViewController

@property (nonatomic, strong) NSObject *myData;


@end

And also, make sure to synthesize the myData property in DestinationViewController.m -

@interface DestinationViewController ()

@end

@implementation DestinationViewController

@synthesize myData = _myData;

// other methods here

@end
1
  • Thanks, but this is almost exactly the same that I do already in the code I've posted. Dec 26, 2012 at 20:01
4

I had this same issue. It turned out for me that the target ViewController property I was setting in my prepareForSegue: code was declared as weak because I had copied and pasted the property from one that InterfaceBuilder auto-created, but mine was not a Storyboard object. So my property was being released and zeroed by ARC on exit from prepareForSegue:. Making it a non-weak property fixed it.

0
0

I had a similar problem where the ViewController I was doing the segue to changed at some odd point, after a looking around a bit it seems that the segue created a new ViewController.

To solve the data passing problem I used notifications.

0

I know this thread is old and solution should be found. But since the final solution is not posted here, I would like to list one of the possible root cause (which is the one in my case). I had the same issue, trying to set the variable in the destination view controller of the segue. The root cause is that I forgot to instantiated ([[Class alloc]init]) the object variable. I need it in my case because I am setting the properties of the object instead of pointing to other object. Hope this help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.