Jag*_*een 24 objective-c uialertview uiactionsheet ios8 uialertcontroller
我有,working iOS application
为了support iOS8,我正在取代UIAlertView/UIActionSheet with
UIAlertController.
问题:
为了显示UIAlertController,我需要UIViewController类的presentViewController方法.
但是 UIAlertView中是从哪个是类显示器inherited的
UIView or NSObject,
我不能[self presentViewController...]为明显的原因的方法.
我的工作:
我尝试获取rootViewController表单当前窗口并显示UIAlertController.
[[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController ...]
Run Code Online (Sandbox Code Playgroud)
但有一些旋转问题,如果我的当前视图控制器没有旋转支持,它将旋转,如果UIAlertController打开.
问题:
有没有人遇到同样的问题并且有安全的解决方案?
如果是,请给我一些例子或给一些指导
Les*_*ter 42
我今天解决了一个基本相似的问题.和Jageen一样,我遇到了一个我希望呈现UIAlertController的情况,但是来自非UIViewController类.就我而言,我希望在运行HTTP请求的故障块时弹出警报.
这就是我使用的,不像我们的朋友,这对我来说非常完美.
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(errorAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Sha*_* TK 15
下面是更好的UIView课程解决方案
的ObjectiveC
UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController.........
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
return topVC;
}
Run Code Online (Sandbox Code Playgroud)
迅速
var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil){
topVC = topVC!.presentedViewController
}
topVC?.presentViewController........
Run Code Online (Sandbox Code Playgroud)
小智 12
我的解决方案如下:
迅速
class alert {
func msg(message: String, title: String = "")
{
let alertView = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
以下是示例用法:
let Alert = alert()
Alert.msg("My alert (without title)")
Alert.msg("This is my alert", title: "Warning!")
Run Code Online (Sandbox Code Playgroud)
Rik*_*les 10
看起来您当前(iOS8之前)从视图对象中触发警报视图.这是非常糟糕的做法,因为一般警报应该从动作和逻辑中触发.并且该代码应该存在于控制器中.
我建议您重构当前的代码,将触发警报的逻辑移动到正确的控制器,然后您可以通过使用self控制器轻松升级到iOS 8 .
相反,如果您从外部对象调用警报,则将控制器传递给调用警报的方法.在上游的某个地方,您必须了解控制器.
对于Swift 4
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
对于Swift 5
UIApplication.shared.windows.last?.rootViewController?.present(alert, animated: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26836 次 |
| 最近记录: |