Benpeterscode
Benpeterscode

Reputation: 93

How to create a pop-up alert once the view loads in xcode

I need a simple alert that pops up when the view loads on the screen. I've found a couple of tutorials on how to create a pop-up alert but they all require that a UIbutton is pressed. I need it to just automatically pop up when the view loads.

This is a start to my code, however I can't figure out how to call upon the code without having to use a UIbutton action:

    override func viewDidLoad() {
    super.viewDidLoad()

    let alertController = UIAlertController(title: "Disclaimer", message:
        "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default,handler: nil))

    self.presentViewController(alertController, animated: true, completion: nil)  

Upvotes: 0

Views: 3418

Answers (1)

matt
matt

Reputation: 534950

alert that pops up when the view loads on the screen

The problem is that "load" does not mean "on the screen". You are conflating two different things. viewDidLoad merely means that the view controller has a view. The view does not appear on the screen until viewDidAppear:. So that's the place to put this code.

Upvotes: 7

Related Questions