如何正确呈现视图控制器? [英] How to present view controller properly?

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

问题描述

我有一个非常简单的应用程序,其中有几个UIViewControllers(例如 VC_A VC_B ).每个屏幕都有一个按钮,可以切换到另一个屏幕(不使用UINavigation).

I have a pretty simple application with a couple of UIViewControllers (say VC_A and VC_B). Every screen has a button that allows to switch to another screen (no UINavigation is used).

App计划一个本地通知,该通知在到期时应显示另一个视图控制器( VC_N -无论当前处于哪个屏幕处于活动状态).

App schedules a local notification, which, when expired, should present another view controller (VC_N - no matter what screen is active at the moment).

问题是有时应用程序会抛出:

The problem is that sometimes application throws:

'NSInternalInconsistencyException', reason: 'Attempting to begin a modal transition from  
<VC_A: 0x2021e0> to <VC_N: 0xf84b970> while a transition is already in progress. 

Wait for viewDidAppear/viewDidDisappear to know the current transition has completed'

实施这种行为的正确方法是什么?

What is the proper way to implement such behaviour?

推荐答案

A.使用UIViewController的新的presentViewController:animated:completion来显示所有三个控制器,而不是旧的模态方法.

A. Use UIViewController's new presentViewController:animated:completion to present all three controllers instead of the old modal method.

B.创建一个布尔标志并将其初始化为NO.

B. Create a Boolean flag and initialize it to NO.

C.在任何视图控制器演示之前,请检查此标志.如果是,则设置为否".然后出示VC.在完成块中,再次将标志设置为是".

C. Before any view controller presentation, check for this flag. If YES, set to NO. And then present the VC. In the completion block, set the flag to YES again.

D.当提供初始视图控制器(VC_A或VC_B)时,应用程序应忽略此标志.

D. The app should ignore this flag when present the initial view controller whether VC_A or VC_B.

一个陷阱是,如果在视图控制器已经处于过渡状态时按下按钮或本地通知过期,则不会显示新的VC.可以改进这种逻辑,以便在需要时提出.

One pitfall to this is that if a button got pressed or the local notification expired while already a view controller was in a transition state, then the new VC won't get presented. One can improve upon this logic to present it after if needed.

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

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