dismissViewControllerAnimated完成方法不起作用

Jon*_*atz 5 objective-c ios

这是我的应用程序设计.我有mainController哪些礼物secondViewController.现在,我想解雇secondViewController并随后调用方法aMethod,mainController如下所示:

[self dismissViewControllerAnimated:YES completion:aMethod];
Run Code Online (Sandbox Code Playgroud)

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

显然我没有正确使用完成处理程序,但我无法弄清楚正确的方法.

iDe*_*Dev 12

我想这就是你要找的东西,

[self dismissViewControllerAnimated:YES completion:^{
            [self.mainController aMethod];
        }];
Run Code Online (Sandbox Code Playgroud)

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

__block SecondViewController *object = self;

[self dismissViewControllerAnimated:YES completion:^{
                [object.mainController aMethod];
            }];
Run Code Online (Sandbox Code Playgroud)

只是为了避免self被阻止.

更新:

现在有问题了.您需要mainController在.h文件中声明为属性secondViewController.在那之后,当你呈现secondViewControllermaincontroller,你需要将其设置为,

secondViewController.maincontroller = self;
[self presentViewController:secondViewController animated:YES completion:Nil];
Run Code Online (Sandbox Code Playgroud)

在你的SecondViewController.h档案中,

@property(nonatomic, assign) MainController *mainController;
Run Code Online (Sandbox Code Playgroud)

在你的SecondViewController.m档案中,

@synthesis mainController;
Run Code Online (Sandbox Code Playgroud)

更新2:

如果您不想声明maincontroller为属性,请尝试此操作.我不确定这是否是正确的做法.但它看起来像以前一样工作.

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

    [self dismissViewControllerAnimated:YES completion:^{
                    [mainController aMethod];
                }];
Run Code Online (Sandbox Code Playgroud)

更新3(建议):

这应该适合你.核实.

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

    [self dismissViewControllerAnimated:YES completion:^{
                    [mainController aMethod];
                }];
Run Code Online (Sandbox Code Playgroud)