Show alertController from custom class

I am trying to show an AlertController from a class that I created. Since AlertController is a subclass of UIResponder, I use the following line of code that Xcode offers me

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

But I can not compile, because AnyClass? has no member nowViewController. My class is a subclass of NSObject.

Any other solution? Thanks

+1
source share
4 answers

Well, you just need to find the topmost controller and submit alertcontrollerfrom there.

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

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

loans

Note: This is the objective-c code version. Remember to convert it to fast.

SWIFT

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

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

"". . , , . : - , .

, , , . , "".

- , , , . ; , " " Cocoa.

+1

From ur view Controller u has a pass-through link Controller, message, header, e.g.

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

where Setting is a subclass of NSObject.In Setting the class u, you must define the method as

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)

}

I represent the UIAlertController.

+1
source

Last speed:

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

All Articles