0

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)  
pnuts
  • 58,317
  • 11
  • 87
  • 139
Benpeterscode
  • 93
  • 2
  • 13

1 Answers1

7

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Awesome! That makes sense! Thank you! – Benpeterscode Feb 09 '16 at 16:29
  • @Benpeterswake http://stackoverflow.com/questions/11254697/difference-between-viewdidload-and-viewdidappear good information on what matt is talking about. You definitely want to understand the view lifecycle fully. – Will M. Feb 09 '16 at 16:30