error with very basic example - swift - Attempt to present on whose view is not in the window hierarchy

error with very basic example - swift - Attempt to present on whose view is not in the window hierarchy



I'm practicing with a basic example of alerts, it's two ViewControllers, each one has a button to go to the next or return, and another button to show an Alert



I added a Simple Alert in the ViewControler1 and it works fine, but if I add a Simple Alert in the ViewControler2 it does not work and shows the following error:



2018-09-12 16: 23: 43.107112-0500 proyect1 [74831: 1130476] Warning: Attempt to present on whose view is not in the window hierarchy!



Code viewController1:


import UIKit

class ViewController1: UIViewController

@IBAction func btnAlerta1 (_ sender: UIButton)
let alert = UIAlertController (style: .alert, title: "Verify your data", message: "Enter your email and password correctly")
alert.addAction (title: "Ok", color: .black, style: .default) action in
alert.show ()

override func viewDidLoad ()
super.viewDidLoad ()





Code viewController2:


import UIKit

class ViewController1: UIViewController

@IBAction func btnAlerta2 (_ sender: UIButton)
let alert = UIAlertController (style: .alert, title: "Verify your data", message: "Enter your email and password correctly")
alert.addAction (title: "Ok", color: .black, style: .default) action in
alert.show ()

override func viewDidLoad ()
super.viewDidLoad ()





And if I go back to the ViewController1 and wish to invoke the Simple Alert that worked before, now it does not work and it presents the same error!



could you help me?






There's no show() method in UIAlertController. If you are using some sort of extension, that's the reason you get such error. Do as in the doc.

– OOPer
Sep 12 '18 at 22:49


show()


UIAlertController






@OOPer .show() used to be a method on a UIAlertView()

– Carpsen90
Sep 12 '18 at 22:54


.show()


UIAlertView()






@Carpsen90, The OP is using UIAlertController, not UIAlertView. The class having show() methods is deprecated and UIAlertController does not have the method named show(). One more, your answer seems to be the right way as documented.

– OOPer
Sep 12 '18 at 22:56



UIAlertController


UIAlertView


show()


UIAlertController


show()






@OOPer exactly, it's probably due to an old tutorial

– Carpsen90
Sep 12 '18 at 22:59






@Carpsen90, just not an old tutorial. Without some extension defining show(), the OP's code does not compile. So, he/she has found a bad site showing the extension which seemingly works in very limited condition. You should better be discouraged to use such a thing.

– OOPer
Sep 12 '18 at 23:04


show()




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)