PresentModalViewController之后如何访问parentViewController?

发布于 2024-12-26 00:10:41 字数 1438 浏览 3 评论 0原文

我需要在presentMoalViewController 之后访问parentViewController。 这是我在第一个视图控制器中调用以查看第二个视图控制器的方法:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠我空喜 2025-01-02 00:10:41

如果您在第二个 viewController 中使用第一个 viewController 可以将其自身设置为的委托/协议,那就更好了。您可以在此处将数据传回之前的控制器或简单地进行搜索来找到更多信息。这种设计模式几乎在 iOS 编程中无处不在。

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

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'.

酒几许 2025-01-02 00:10:41

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

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

在你的父 vc 视图中 didload:

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

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];
度的依靠╰つ 2025-01-02 00:10:41

对于 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< /code> 您声明方法的类

- (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];
这应该有效,如果您有任何疑问,请随时询问!

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!

も星光 2025-01-02 00:10:41

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

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文