我需要在presentMoalViewController之后访问parentViewController。
这是我在firstViewController中调用以查看secondViewController的方法:

- (void)viewData {

    SecondViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"select_data"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
    navigationController.navigationBar.barStyle = UIBarStyleBlack;

    UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(saveData)] autorelease];
    viewCtrl.navigationItem.rightBarButtonItem = salvaButton;

    UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:@selector(backView)] autorelease];
    viewCtrl.navigationItem.leftBarButtonItem = annullaButton;

    [self presentModalViewController:navigationController animated:YES];

}


当我单击saveButton时,我尝试以这种方式访问​​parentViewController,但它不起作用:

- (void) saveData {

    FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
    parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"];
    [parentView performSelectorInBackground:@selector(myMethod) withObject:nil];

    [self dismissModalViewControllerAnimated:YES];

}

最佳答案

如果在第二个viewController中使用委托/协议,则第一个viewController可以将自己设置为。您可以在Pass data back to the previous controller处找到更多信息,或者只是进行搜索。在iOS编程中几乎所有地方都使用了这种设计模式。

最大的好处是,您的第二个viewController变得独立于提供它的人,并且可以轻松地重用。技术术语为“去耦”。

关于iphone - presentModalViewController之后如何访问parentViewController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8788294/

10-10 19:33