从自定义类显示alertController [英] show alertController from a custom class

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

问题描述

我正在尝试显示我所制作的类中的AlertController。
因为AlertController是UIResponder的子类,所以我使用Xcode向我建议的以下代码行

I'm trying to show an AlertController from a class that I've made. Since AlertController is a subclass of UIResponder I'm using the following line of code that Xcode is suggesting me

superclass?.presentViewController(alertController, animated: true, completion: nil)

但是我无法编译,因为AnyClass吗?没有任何成员presentViewController。
我的类是NSObject的子类。

But I cannot compile because AnyClass? does not have any member presentViewController. My class is a subclass of NSObject.

还有其他解决方案吗?
谢谢

Any other solution? Thanks

推荐答案

好吧,您只需要找到最顶层的视图控制器并显示 alertcontroller 。

Well you just need to find the topmost view controller and present the alertcontroller from there.

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}
[topController presentViewController:alertController animated:YES completion:nil];

积分

注意:这是该代码的Objective-C版本。

Note: This is objective-c version of the code. Kindly convert it to swift accordingly.

SWIFT

let topController = UIApplication.sharedApplication().keyWindow!.rootViewController as UIViewController

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}
topController.presentViewController(alertController, animated:true, completion:nil)

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

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