segue: destination view controller weirdness

有些话、适合烂在心里 提交于 2019-12-05 06:05:18

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!