如何在presentModalViewController之后访问parentViewController?

时间:2012-01-09 12:16:03

标签: iphone objective-c ios5 presentmodalviewcontroller

我需要在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];

}

4 个答案:

答案 0 :(得分:1)

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

最大的好处是,您的第二个viewController将独立于呈现它的人,并且可以轻松地重用。技术术语是“脱钩”。

答案 1 :(得分:1)

上次我这样做时,我使用了通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"saveData" object:dataString];

在您的父vc视图didload中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveDataNow:) name:@"saveData" object:nil];

答案 2 :(得分:0)

对于navigationController,您可以声明一个名为myController的属性,然后您必须像这样发起navigationController

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.myController = self;

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];

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];
}

navigationController中,您可以拨打[myController saveData]; 这应该有用,如果您有任何问题,请随时提出!

答案 3 :(得分:0)

您可以使用以下方法访问父视图控制器 -

- (void) saveData {
NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy];
FirstViewController *parentView;
for(int i = 0, i <[activeControllerArray  count], i++) {
    if([[activeControllerArray objectAtIndex:i] isKindOfClass:[FirstViewController class]) {
        parentView= [activeViewController objectAtIndex:i];
        break;
     }
}

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

[self dismissModalViewControllerAnimated:YES];

}