Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Keith Michelson
3,395 PointsAlert popup class utility
Anyone know how to make an alert work in a separate class? Basically like a utility class. I've got the code below for a popup but when trying to make it in a class/struct I'm not sure what to put in place of "self." or how to target it correctly.
let alert = UIAlertController(title: "Error in Form", message: "Please enter a username and password", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})))
self.presentViewController(alert, animated: true, completion: nil)
Thanks for any help.
Keith
6 Answers

Michael Hulet
47,906 PointsIf you're trying to present a UIAlertController
from something other than a UIViewController
subclass, you need to know what UIViewController
to present the UIAlertController
on top of. I think this will do what you're trying to do:
func presentErrorAlertOnViewController(vc: UIViewController) -> Void{
let alert = UIAlertController(title: "Error in Form", message: "Please enter a username and password", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Action", style: .Default, handler: {(action: UIAlertAction) -> Void in
vc.dismissViewControllerAnimated(true, completion: nil)
}))
vc.presentViewController(alert, animated: true, completion: nil)
}

Keith Michelson
3,395 PointsThank you Michael Hulet Then do I just pass self for vc when I call the func? That did work, just want to make sure that's the right thing.

Michael Hulet
47,906 PointsThat depends on if you're calling the function from a view controller. If you are, then passing self
is fine. Otherwise, you'll need to pass in a UIViewController

Keith Michelson
3,395 PointsYeah, I am in this instance but that's good to know as i'll probably do it both ways at one point.
Thanks again.

Keith Michelson
3,395 PointsMichael Hulet Does this only work in an IBAction? I tried it in another function but nothing comes up.

Michael Hulet
47,906 PointsThe view controller you're passing into the function must be visible onscreen. Otherwise, it should work just fine

Keith Michelson
3,395 PointsThanks for the reply. I'm checking it right after viewDidLoad()
let alert = Utils()
alert.displayAlert(self, title: "Error in Form", message: "Please enter a username and password")

Michael Hulet
47,906 PointsTry putting it in viewDidAppear
instead of viewDidLoad

Keith Michelson
3,395 PointsThanks Michael Hulet. that makes sense. I was calling it before it was there. If you have time or know of a link, how would you pass in a UIViewController?