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)
But I cannot compile because AnyClass? does not have any member presentViewController.
My class is a subclass of NSObject.
Any other solution?
Thanks
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];
credits
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)
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.
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
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.e presenting UIAlertController.
Latest Swift:
var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while ((topController.presentedViewController) != nil) {
topController = topController.presentedViewController!;
}
topController.present(alertController, animated:true, completion:nil)