在Segue中设置属性但在ViewDidLoad中返回null

时间:2015-09-16 14:13:28

标签: ios objective-c

好的,这让我很头疼。我有ViewController(使用replace),我分配了它的委托属性

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"spoSelectionDemoPlanSegue"])
  {
    SPOSelectionViewController * vc = [segue destinationViewController];
    vc.delegate = self;
    NSLog(@"spo segue %@", vc.delegate);  //returns current ViewController
  }
}

SPOSelectionViewController.h

@property (nonatomic, assign) id<SPOInputDelegate> delegate;

SPOSelectionViewController.m

- (void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"delegate didload spo %@", delegate);  //returns (null)
}

不知怎的,它打印出来了!但是我的其他ViewController(使用popover)尝试分配其委托,而不是null。有人可以给我建议吗?

1 个答案:

答案 0 :(得分:1)

正如我所料,这很愚蠢,因为故事板的segue是一个navigationController,所以实际上destinationViewController是navigationController而不是SPOSelectionViewController

所以我通过这种方式解决了它:

UINavigationController* navController = segue.destinationViewController;
SPOSelectionViewController * vc = (SPOSelectionViewController*)navController.topViewController;
vc.delegate = self;