【问题标题】:How to pass reference to object to a modally presented view controller that is contained inside a navigation controller?如何将对对象的引用传递给包含在导航控制器中的模态呈现的视图控制器?
【发布时间】:2023-03-15 02:27:01
【问题描述】:

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

介绍 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.
}

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

【问题讨论】:

    标签: ios objective-c uinavigationcontroller modalviewcontroller presentmodalviewcontroller


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多