【问题标题】:show alertController from a custom class从自定义类中显示 alertController
【发布时间】:2015-01-01 21:57:01
【问题描述】:

我正在尝试从我创建的一个类中显示一个 AlertController。 由于 AlertController 是 UIResponder 的子类,因此我正在使用 Xcode 建议的以下代码行

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

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

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

【问题讨论】:

  • presentViewControllerUIViewController 的方法,而不是 NSObject 的方法
  • @Krumelur 我知道,但我正试图从我的班级展示一个 UIAlertController。还有其他解决方法吗?

标签: ios swift alert nsobject uialertcontroller


【解决方案1】:

你只需要找到最顶层的视图控制器并从那里显示alertcontroller

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

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

credits

注意:这是objective-c 版本的代码。请相应地将其转换为swift。

SWIFT

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

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

【讨论】:

  • 抱歉,我要求 swift 并且我不知道如何处理 Obj C。请考虑使用 swift 语言进行修改
【解决方案2】:

问题在于您对“来自”的理解。界面中某个视图的前面会出现一条警报。因此,我们需要知道 what 视图。答案是:某个视图控制器的主视图——主视图在界面中的视图控制器。

因此,只有主视图在界面中的视图控制器才能被告知呈现警报。您必须提供“来自”的视图控制器。

您需要通过某种方式从代码所在的任何位置获取对该视图控制器的引用,以便您的代码可以告诉该视图控制器显示警报。这本身就是一个有趣的问题。事实上,“获取引用”到现有对象是 Cocoa 编程艺术的主要部分。

【讨论】:

  • 这是一个非常重要的问题(“获得参考”),因此我用我的书的一部分来讨论它:apeth.com/iOSBook/ch13.html#_instance_visibility它可能会给你一些想法。
  • 天才!只需在 init 方法中包含 UIViewController 即可。完成。
【解决方案3】:

从你的视图控制器你已经传递了控制器的参考,消息,标题,如

Settings.getAlertViewConroller(self, DialogTitle: "Test Sale", strDialogMessege: "You are performing a test sale. This is not a real transaction.")

其中 Setting 是 NSObject 的子类。在 Setting 类中,您必须将方法定义为

class func getAlertViewConroller(globleAlert:UIViewController,DialogTitle:NSString,strDialogMessege:NSString){


    let actionSheetController: UIAlertController = UIAlertController(title: DialogTitle, message: strDialogMessege, preferredStyle: .Alert)


    let nextAction: UIAlertAction = UIAlertAction(title: "OK", style: .Default) { action -> Void in

    }
    actionSheetController.addAction(nextAction)

    globleAlert.presentViewController(actionSheetController, animated: true, completion:nil)

}

即呈现 UIAlertController。

【讨论】:

    【解决方案4】:

    最新的斯威夫特:

        var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
        while ((topController.presentedViewController) != nil) {
            topController = topController.presentedViewController!;
        }
        topController.present(alertController, animated:true, completion:nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 2018-12-02
      • 2012-03-19
      相关资源
      最近更新 更多