Swift 3 - How to make an alert appear after and load screen?

Asked

Viewed 356 times

0

Hello I am building an app that needs an alert to be presented as soon as the User accesses the screen.

For this I made 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’re right, but always present the message on the console:

2017-06-09 11:30:53.377951-0300 Issoebrasil[599:216652] Warning: Attempt to present on Whose view is not in the window Hierarchy!

2 answers

0


Good afternoon Matheus :)
I put your code here and it’s really giving that message...
Not to stall too much I’ll spend 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 Linen Linen :)

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 that’s what you’re looking for.. :)
Hugs

0

Good afternoon (again :) Matheus..
Another way to do what you want is to add 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 fired without the mentioned error..
I put this because this could be your need..
Hugs.. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.