显示和关闭模态视图控制器
任何人都可以给我示例代码,我可以使用它首先呈现模式视图控制器,然后关闭它?这就是我一直在尝试的:
NSLog(@"%@", blue.modalViewController);
[blue presentModalViewController:red animated:YES];
NSLog(@"%@", blue.modalViewController);
[blue dismissModalViewControllerAnimated:YES];
NSLog(@"%@", blue.modalViewController);
此代码位于 viewDidLoad 中(“蓝色”和“红色”都是 UIViewController 的子类)。我希望我会显示红色视图,然后立即隐藏它,并带有一些动画。然而,这段代码仅呈现模态视图,并没有关闭它。有什么想法吗?第一个日志显示“null”,而另外两个日志显示
另一点是,如果我将此代码放入 applicationDidFinishLaunching: 红色视图根本不会出现,并且所有日志得到“空”
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);
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,当您将该代码放入 applicationDidFinishLaunching 中时,从 Interface Builder 实例化的控制器可能尚未链接到您的应用程序(因此“红色”和“蓝色”仍然
nil
) 。但要回答您最初的问题,您做错的事情是您在错误的控制器上调用
dismissModalViewControllerAnimated:
!它应该是这样的:通常“红色”控制器应该决定在某个时刻解雇自己(也许当单击“取消”按钮时)。然后“红色”控制器可以调用
self
上的方法:如果仍然不起作用,则可能与控制器以动画方式呈现有关,因此您可以不允许在呈现控制器后这么快就将其解散。
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
).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: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
: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.
Swift
已更新为 Swift 3
故事板
创建两个视图控制器,每个控制器上都有一个按钮。对于第二个视图控制器,将类名设置为
SecondViewController
,将 Storyboard ID 设置为secondVC
。代码
ViewController.swift
SecondViewController.swift
来源:
Swift
Updated for Swift 3
Storyboard
Create two View Controllers with a button on each. For the second view controller, set the class name to
SecondViewController
and the storyboard ID tosecondVC
.Code
ViewController.swift
SecondViewController.swift
Source:
我在 xcode 4.52 中感到厌倦的最简单方法是创建一个附加视图并使用 segue 模式连接它们(控制将按钮从视图一拖到第二视图,选择模态)。
然后将按钮拖入第二个视图或您创建的模式视图。控制并拖动该按钮到头文件中并使用动作连接。这将在您的controller.m 文件中创建一个IBaction。在代码中找到您的按钮操作类型。
The easiest way i tired in xcode 4.52 was to create an additional view and connect them by using segue modal(control drag the button from view one to the second view, chose Modal).
Then drag in a button to second view or the modal view that you created. Control and drag this button to the header file and use action connection. This will create an IBaction in your controller.m file. Find your button action type in the code.
PresentModalViewController:
解雇ModalViewController:
presentModalViewController:
dismissModalViewController:
最简单的方法是使用 Storyboard 和 Segue。
只需创建一个从 TabBarController 的 FirstViewController(不是导航控制器)到具有登录 UI 的 LoginViewController 的 Segue,并将其命名为“showLogin”。
创建一个返回 BOOL 的方法来验证用户登录和/或其会话是否有效......最好在 AppDelegate 上。称其为 isSessionValid。
在您的 FirstViewController.m 上重写 viewDidAppear 方法,如下所示:
然后,如果用户成功登录,只需关闭或弹出 LoginViewController 以显示您的选项卡。
100%有效!
希望有帮助!
The easiest way to do it is using Storyboard and a Segue.
Just create a Segue from the FirstViewController (not the Navigation Controller) of your TabBarController to a LoginViewController with the login UI and name it "showLogin".
Create a method that returns a BOOL to validate if the user logged in and/or his/her session is valid... preferably on the AppDelegate. Call it isSessionValid.
On your FirstViewController.m override the method viewDidAppear as follows:
Then if the user logged in successfully, just dismiss or pop-out the LoginViewController to show your tabs.
Works 100%!
Hope it helps!
Swift
self.dismissViewControllerAnimated(true,完成:nil)
Swift
self.dismissViewControllerAnimated(true, completion: nil)