从任何地方以模态方式呈现 UIViewController 的推荐方式是什么?
在我编写的许多应用程序中,多次发生这样的情况:我必须从某个类中以模态方式呈现控制器,而在这些类中我没有引用当前的可见控制器。 (例如,一个 StoreKit 事务观察器,它提供一个用于显示产品下载进度的控制器)。
有了 Three20,我可以使用 - [TTNavigatorvisibleViewController]
来完成工作,但是如果我不想使用该框架怎么办?我应该重新实现一个类似于 TTNavigator
的自定义函数吗? Apple API 中是否有类似的方法?例如,为我的应用程序中的所有控制器创建一个公共父类,然后使用 NSNotificationCenter 来处理所有应用程序范围的通知是否会更好? (这当然会产生在一个类中包含不相关代码的副作用)
我真的很惊讶 Apple 没有在他的标准 API 中提供这一基本功能。或者也许这个问题反复出现在我身上是设计实践不佳的标志?
In many applications I wrote it occurred many times that I had to present a controller modally from some class where I had no reference to the current visible controller. (e.g. A StoreKit transaction observer that presents a controller used to display download progress of the products).
With Three20, I can use the - [TTNavigator visibleViewController]
to get the job done, but what if I don't want to use the framework? Should I reimplement a custom function that acts like TTNavigator
? Is there maybe a similar method in Apple APIs? Could it be better to, for example, create a common parent class for all the controllers in my application and then use NSNotificationCenter
to handle all the application-wide notifications? (This of course would have the side effect of having unrelated code in one class)
I'm really surprised Apple didn't provide this basic functionality in his standard APIs. Or maybe the fact that this problem is recurring to me is a sign of poor design practices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些想法,不一定保证是正确的,但也许会让你走向正确的方向:如果我正确理解你的问题,基本上你有一些具体的行动,在某个时刻(比如完成后)需要显示模式视图,但是所述操作没有对当前屏幕上的视图控制器的引用(例如,也许您正在后台执行任务并让用户仍然浏览您的程序,并希望在完成时提醒用户) 。
我想您将如何解决这个问题取决于您的应用程序的整体架构。大多数应用程序都会有一些控制导航的根视图控制器:也许您有一个在应用程序委托中创建的 UITabBarController ,并且所有导航都来自该控制器。您可以通过此顶级视图控制器显示模态视图控制器(您可能会在应用程序委托内执行此操作)。
或者,您可以放弃显示模式视图控制器的想法,并将视图直接附加到应用程序窗口。如果您查看相当流行的库(例如
MBProgressHUD
),您会发现可以将提供的模式加载视图附加到应用程序窗口,所有视图控制器都位于该窗口中。因此,有许多不同的策略可以实现您想要的目标。我不一定会说您的问题是糟糕的应用程序设计的结果,因为在某些情况下您可能需要显示一些模式对话框并且不知道当前可见什么视图控制器。也就是说,在您给出的示例中 - 显示下载进度的 StoreKit 事务观察器 - 人们会假设它会在离散操作(例如购买产品)之后被触发,并且您会知道是从哪个视图控制器触发的。
A few thoughts, not necessarily guaranteed to be correct but perhaps will get you in the right direction: If I understand your problem correctly, basically you have some concrete action that at some point (say upon completion) needs to display a modal view, but said action doesn't have a reference to the view controller that's currently on the screen (for example, maybe you're doing a task in the background and letting the user still navigate through your program, and want to alert the user on completion).
I guess how you would work around this would depend on the overall architecture of your application. Most apps will have some root view controller that controls navigation: perhaps you've got a
UITabBarController
that you create in your app delegate, and all navigation come off that controller. You could just display your modal view controller through this top level view controller (you'd probably be doing this inside your app delegate).Alternatively, you can let go of the idea of displaying modal view controllers and attach the view directly to your application window. If you look at a fairly popular library such as
MBProgressHUD
you'll see you can attach the modal loading views offered to the app window, in which all your view controllers sit.So there are a number of different strategies for achieving what you want. I wouldn't necessarily say your problem is the result of a poor app design, since there are scenarios when you could conceivably need to display some modal dialog and not know what view controller was currently visible. That said, in the example you give - a StoreKit transaction observer that shows download progress - one would assume it would be triggered after a discrete action (purchasing a product, for example), and you would know which view controller that had been triggered from.