gpt4 book ai didi

iphone - PresentModalViewController之后如何访问parentViewController?

转载 作者:行者123 更新时间:2023-12-03 21:04:30 25 4
gpt4 key购买 nike

我需要在presentMoalViewController之后访问parentViewController。这是我在第一个 View Controller 中调用以查看第二个 View Controller 的方法:

- (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 可以将其自身设置为的委托(delegate)/协议(protocol),那就更好了。您可以在这里找到更多信息Pass data back to the previous controller或者只是进行搜索。这种设计模式几乎在 iOS 编程中随处可见。

最大的优点是,你的第二个 viewController 变得独立于呈现它的人,并且可以轻松地重用。技术术语是“脱钩”。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com