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! While you're at it, check out some resources Treehouse students have shared here.

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

iOS

Keith Michelson
Keith Michelson
3,395 Points

Alert 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
Michael Hulet
47,912 Points

If 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
Keith Michelson
3,395 Points

Thank 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
Michael Hulet
47,912 Points

That 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
Keith Michelson
3,395 Points

Yeah, 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
Keith Michelson
3,395 Points

Michael Hulet Does this only work in an IBAction? I tried it in another function but nothing comes up.

Michael Hulet
Michael Hulet
47,912 Points

The view controller you're passing into the function must be visible onscreen. Otherwise, it should work just fine

Keith Michelson
Keith Michelson
3,395 Points

Thanks 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
Michael Hulet
47,912 Points

Try putting it in viewDidAppear instead of viewDidLoad

Keith Michelson
Keith Michelson
3,395 Points

Thanks 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?