How to access to parentViewController after presen

2019-07-21 03:25发布

I need to access to parentViewController after presentMoalViewController. This is my method that I call in the firstViewController to view the 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];

}

When I click on saveButton, I try to access to parentViewController in this way, but it not work:

- (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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-21 04:04

For navigationController you can declare a property called something like myController, then you have to initiate your navigationController like this:

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

In your parentViewController Class you declare your Method

- (void) saveData {

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

[self dismissModalViewControllerAnimated:YES];
}

In your navigationController you can then call [myController saveData]; This should work, if you have any questions, feel free to ask!

查看更多
做个烂人
3楼-- · 2019-07-21 04:10

It would be much better if you used a delegate/protocol in your second viewController that the first viewController could set itself as. You can find more info here Pass data back to the previous controller or simply doing a search. This design pattern is used virtually everywhere in iOS programming.

The big advantage is that then your second viewController becomes independent of whoever presented it, and can be easily reused. The technical term would be 'decoupling'.

查看更多
相关推荐>>
4楼-- · 2019-07-21 04:27

Last time I did this I used notifications:

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

in your parent vc view didload:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveDataNow:) name:@"saveData" object:nil];
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-07-21 04:28

You can access your parent view controller using following-

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

}
查看更多
登录 后发表回答