简体   繁体   English

从自定义类显示alertController

[英]show alertController from a custom class

I'm trying to show an AlertController from a class that I've made. 我正在尝试从我制作的类中显示一个AlertController。 Since AlertController is a subclass of UIResponder I'm using the following line of code that Xcode is suggesting me 由于AlertController是UIResponder的子类,所以我使用Xcode向我建议的以下代码行

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

But I cannot compile because AnyClass? 但是我无法编译,因为AnyClass吗? does not have any member presentViewController. 没有任何成员presentViewController。 My class is a subclass of NSObject. 我的类是NSObject的子类。

Any other solution? 还有其他解决方案吗? Thanks 谢谢

Well you just need to find the topmost view controller and present the alertcontroller from there. 好吧,您只需要找到最顶层的视图控制器并从那里显示alertcontroller

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

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

credits 学分

Note: This is objective-c version of the code. 注意:这是该代码的Objective-C版本。 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)

The problem is your understanding of "from". 问题是您对“来自”的理解。 An alert appears in front of some view in the interface. 警报会出现在界面中某些视图的前面。 Thus, we need to know what view. 因此,我们需要知道什么看法。 The answer is: the main view of some view controller - a view controller whose main view is in the interface. 答案是:某些视图控制器的主视图-主视图位于界面中的视图控制器。

Thus, only a view controller whose main view is in the interface can be told to present an alert. 因此,只有其主视图位于界面中的视图控制器才能被告知发出警报。 It is that view controller that you must present "from". 您必须“从”显示该视图控制器。

You'll need to have some way of getting a reference to that view controller from wherever your code is, so that your code can tell that view controller to present the alert. 无论代码位于何处,您都需要某种方式来获取对该视图控制器的引用,以便您的代码可以告诉该视图控制器呈现警报。 That in itself can be an interesting problem; 这本身可能是一个有趣的问题。 indeed, "getting a reference" to an existing object is a major part of the art of Cocoa programming. 实际上,“获得对现有对象的引用”是Cocoa编程技术的主要部分。

From ur view Controller u have pass Controller's reference ,message ,title such as 从您的观点来看,控制器您已经传递了控制器的参考,消息,标题,例如

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

where Setting is the subclass of NSObject.In Setting class u have to define method as 其中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)

}

ie presenting UIAlertController. 即介绍UIAlertController。

Latest Swift: 最新的Swift:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM