呈现和关闭模态视图控制器 [英] Present and dismiss modal view controller

查看:23
本文介绍了呈现和关闭模态视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个示例代码,我可以用它来首先展示一个模态视图控制器,然后关闭它?这就是我一直在尝试的:

Can anyone give me the example code that I can use to first present a modal view controller, then dismiss it? This is what I have been trying:

NSLog(@"%@", blue.modalViewController);
[blue presentModalViewController:red animated:YES];
NSLog(@"%@", blue.modalViewController);
[blue dismissModalViewControllerAnimated:YES];
NSLog(@"%@", blue.modalViewController);

这段代码在 viewDidLoad 中(blue"和red"都是 UIViewController 的子类).我希望我会显示红色视图,然后立即隐藏它,并带有一些动画.然而,这段代码只显示了模态视图,并没有关闭它.任何的想法?第一个日志显示null",而其他两个日志显示

This code is in viewDidLoad ("blue" and "red" are both subclasses of UIViewController). I expect that I will show the red view and then immediately hide it, with some animation. However this piece of code only presents the modal view and does not dismiss it. Any idea? The first log shows "null" while the two other logs show <RedViewController: 0x3d21bf0>

还有一点是,如果我把这段代码放在applicationDidFinishLaunching中:红色视图根本不出现,所有日志都变成空"

Another point is, if I put this code in applicationDidFinishLaunching: the red view does not appear at all, and all logs get "null"

推荐答案

首先,当您将该代码放入 applicationDidFinishLaunching 时,可能会出现这样的情况,即从 Interface Builder 实例化的控制器尚未链接到您的应用程序(因此"red" 和 "blue" 仍然是nil).

First of all, when you put that code in applicationDidFinishLaunching, it might be the case that controllers instantiated from Interface Builder are not yet linked to your application (so "red" and "blue" are still nil).

但是要回答您最初的问题,您做错了什么是您在错误的控制器上调用了 dismissModalViewControllerAnimated:!应该是这样的:

But to answer your initial question, what you're doing wrong is that you're calling dismissModalViewControllerAnimated: on the wrong controller! It should be like this:

[blue presentModalViewController:red animated:YES];
[red dismissModalViewControllerAnimated:YES];

通常,红色"控制器应该决定在某个时候关闭自己(也许当点击取消"按钮时).然后红色"控制器可以调用 self 上的方法:

Usually the "red" controller should decide to dismiss himself at some point (maybe when a "cancel" button is clicked). Then the "red" controller could call the method on self:

[self dismissModalViewControllerAnimated:YES];

如果它仍然不起作用,则可能与控制器以动画方式呈现的事实有关,因此您可能不允许在呈现控制器后很快关闭它.

If it still doesn't work, it might have something to do with the fact that the controller is presented in an animation fashion, so you might not be allowed to dismiss the controller so soon after presenting it.

这篇关于呈现和关闭模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆