从UIView / NSObject类显示UIAlertController [英] Display UIAlertController from UIView/NSObject class

查看:465
本文介绍了从UIView / NSObject类显示UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 正在运行的iOS应用程序
为了 支持iOS8 ,我用
UIAlertController取代 UIAlertView / UIActionSheet

I have working iOS application In order to support iOS8, I am replacing UIAlertView/UIActionSheet with UIAlertController.

问题:

对于显示UIAlertController,我需要UIViewController类的presentViewController
方法。

但是 UIAlertView显示来自 继承的类 来自
UIView或NSObject

由于显而易见的原因,我无法获得 [self presentViewController ...] 方法。

Problem :
For display UIAlertController I need presentViewController method of UIViewController class.
But UIAlertView is display from classes which are inherited from UIView or NSObject,
I can not get [self presentViewController...] method for obvious reason.

我的工作:

我尝试从当前窗口获取rootViewController并显示UIAlertController。

My Work :
I tried getting rootViewController form current window and display UIAlertController.

[[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController ...]

但是有一些旋转问题,比如我当前的视图控制器没有旋转支持
如果UIAlertController打开,它将会轮换。

but have some rotation problems like if my current view controller do not have rotation support it will rotate if UIAlertController is open.

问题:

是否有人遇到同样的问题并且有安全解决方案

如果是,请提供一些示例或给出一些指导

Question :
Did any one faced same problem and have safe solution ?
if yes please provide me some example or give some guide

推荐答案

看起来你现在(在iOS8之前)从视图对象中触发警报视图。这是非常糟糕的做法,因为一般警报应该从动作和逻辑中触发。并且该代码应该存在于控制器中。

It looks like you are currently (pre-iOS8) triggering an alert view from within your view object. That's pretty bad practice, as in general alerts should be triggered from actions and logic. And that code should live in controllers.

我建议您重构当前代码,将触发警报的逻辑移动到正确的控制器,然后您可以轻松升级到iOS 8使用 self 作为控制器。

I suggest you refactor your current code to move the logic that triggers the alert to the correct controller, and then you can easily upgrade to iOS 8 by using self as the controller.

如果您正在从外部对象调用警报,然后将控制器传递给调用警报的方法。在上游的某个地方,您必须了解控制器。

If instead you're calling the alert from an outside object, then pass in the controller to the method that calls the alert. Somewhere upstream you must have knowledge of the controller.

这篇关于从UIView / NSObject类显示UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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