Swift 3 - How to make an alert appear after the screen loads?

0

Hello, I'm building an app that requires an alert to appear as soon as the user accesses the screen.

For this I did a ViewController and linked the view that is being accessed

class EducacaoController : UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let alert = UIAlertController(title: "Atenção!", message: "oi", preferredStyle: .alert)

        let acaoAvançar = UIAlertAction(title: "Avançar", style: .default, handler: nil)

        alert.addAction(acaoAvançar)

        self.present(alert, animated: true, completion: nil)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Apparently I believe you are right, but always present the message on the console:

2017-06-09 11: 30: 53.377951-0300 EsoEBrasil [599: 216652] Warning: Attempt to present on whose view is not in the hierarchy!

    
asked by anonymous 09.06.2017 / 16:32

2 answers

0

Good afternoon Matheus :)
I put your code here and it's really giving this message ...
Not to curl too much I'll pass as I do:

func Mensagem(_ strTitle : NSString, strBody: NSString, delegate: AnyObject?)
    {

        let valert : UIAlertView = UIAlertView();
        valert.message  = strBody as String;
        valert.title    = strTitle as String;
        valert.delegate = delegate;
        valert.addButton(withTitle: "OK");
        valert.show();

    }

To use is mill mill:)

override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        Mensagem("Ola", strBody: "VAI", delegate: nil);



    }

I hope this is what you're looking for .. :)
Hugs

    
09.06.2017 / 18:13
0

Good afternoon (again :)) Matheus ..
Another way to do what you want is by adding the viewDidAppear:

override func viewDidAppear(_ animated: Bool)
    {
        let alert = UIAlertController(title: "Atenção!", message: "oi", preferredStyle: .alert)

        let acaoAvançar = UIAlertAction(title: "Avançar", style: .default, handler: nil)

        alert.addAction(acaoAvançar)



        self.present(alert, animated: true, completion: nil)

    }

It's your code only in a different place. If you test you will see that every time the screen APPEARS the message is triggered without the error mentioned ..
I put this because this could be your need ..
Hugs ..:)

    
09.06.2017 / 19:21