如何将对象的引用传递给导航控制器中包含的模态呈现的视图控制器?

时间:2013-03-18 00:10:09

标签: ios objective-c uinavigationcontroller modalviewcontroller presentmodalviewcontroller

我在向我的数据对象传递引用时遇到问题。呈现视图控制器具有对数据对象的引用。模态视图控制器连接到导航控制器,并且是其根视图控制器。我正是这样做的:

介绍VC:

- (IBAction)changeCustomerButtonPress:(UIButton *)sender {    
    UINavigationController *customersNC = [self.storyboard instantiateViewControllerWithIdentifier:@"customersNC"];
    SCCustomersVC *customersVC = (SCCustomersVC *)[self.storyboard instantiateViewControllerWithIdentifier:@"customersVC"];
    customersVC.dataObject = self.splitVC.dataObject;

    //at this point, customersVC.dataObject exists

    [self presentViewController:customersNC animated:YES completion:nil];
}

出现模态VC时,self.dataObject为nil。

- (void)viewWillAppear:(BOOL)animated
{       
    //self.dataObject is nil here.
}

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

IB中的导航控制器是否有根视图控制器?如果,不应该。但是假设你有一个,你不应该实例化SCCustomersVC,因为导航控制器在实例化时会这样做。要传递数据,只需使用topViewController获取对该控制器的引用:

SCCustomersVC *customersVC = (SCCustomersVC *)[customerNC topViewController];
    customersVC.dataObject = self.splitVC.dataObject;