草庐IT

objective-c - dismissViewControllerAnimated 完成方法不起作用

coder 2023-09-24 原文

这是我的应用程序设计。我有 mainController,它显示 secondViewController。现在,我想关闭 secondViewController,然后在 mainController 上调用方法 aMethod,如下所示:

[self dismissViewControllerAnimated:YES completion:aMethod];

但这给了我错误use of undeclared identifier 'aMethod'

显然我没有正确使用完成处理程序,但我想不出正确的方法。

最佳答案

我想这就是你要找的,

[self dismissViewControllerAnimated:YES completion:^{
            [self.mainController aMethod];
        }];

在上面的代码中,您需要在 block 外声明self 并将其用作,

__block SecondViewController *object = self;

[self dismissViewControllerAnimated:YES completion:^{
                [object.mainController aMethod];
            }];

只是为了避免 self 被保留在 block 中。

更新:

现在有问题了。您需要在 secondViewController 的 .h 文件中将 mainController 声明为一个属性。之后,当您从 maincontroller 呈现 secondViewController 时,您需要将其设置为,

secondViewController.maincontroller = self;
[self presentViewController:secondViewController animated:YES completion:Nil];

在你的SecondViewController.h文件中,

@property(nonatomic, assign) MainController *mainController;

在你的 SecondViewController.m 文件中,

@synthesis mainController;

更新 2:

如果您不想将 maincontroller 声明为一个属性,试试这个。我不确定这是否是正确的做法。但看起来它曾经有效。

 MainController *mainController = (MainController *)[self.view.superview nextResponder];

    [self dismissViewControllerAnimated:YES completion:^{
                    [mainController aMethod];
                }];

更新 3(建议):

这应该适合您。检查一下。

 MainController *mainController = (MainController *)self.parentViewController;

    [self dismissViewControllerAnimated:YES completion:^{
                    [mainController aMethod];
                }];

关于objective-c - dismissViewControllerAnimated 完成方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079068/

有关objective-c - dismissViewControllerAnimated 完成方法不起作用的更多相关文章

随机推荐