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

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

问题描述

任何人都可以给我一些示例代码,我可以用它来首先呈现一个模态视图控制器,然后解雇它吗?这就是我一直在尝试的:

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,而另外两个日志显示< RedViewController:0x3d21bf0>

另一点是,如果我将此代码放在applicationDidFinishLaunching中:红色视图根本不显示,所有日志获取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>
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尚未链接到您的应用程序(因此红色和蓝色仍然 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天全站免登陆