0

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?

Zeus Tune
  • 11
  • 4
  • 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](https://developer.apple.com/documentation/uikit/uialertcontroller). – OOPer Sep 12 '18 at 22:49
  • @OOPer `.show()` used to be a method on a `UIAlertView()` – ielyamani Sep 12 '18 at 22:54
  • @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
  • @OOPer exactly, it's probably due to an old tutorial – ielyamani 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
  • I agree with the other comments. This plainly is wrong, probably because is both outdated and, well, because there was never anything like `show`. One thing not mentioned though - and I think quite helpful in the end is understanding view hierarchy. VC#1 "presents" VC#2, which in turn *usually* should only "present" VC#3, not VC#1. Depending on what you are really trying to do, you may wish to **push** VC#2 and **pop** back to VC#1, which is easily done via a `UINavigationController`. You can also **present** VC#2 from VC#1 and most times if VC#1 is a delegate of VC#2, acheive what you want. –  Sep 12 '18 at 23:36
  • Friends, thank you very much for your comments. The library that I try to use is Alerts & Pickers, this is the link https://github.com/dillidon/alerts-and-pickers, it seems very well documented, it was updated 8 months ago. But I think what causes the error is the view hierarchy. – Zeus Tune Sep 13 '18 at 00:01
  • @dfd, as you mention, I think I'm not invoking the ViewController2 alert correctly, but I don't know how to do it – Zeus Tune Sep 13 '18 at 00:07
  • @ZeusTune, its initial commit was 9 months ago and its last update was 8 months ago. One month may not be enough to refine a library and well documented does not mean it's well-coded. I strongly recommend not to use such library. What causes the error is that the library is not well-coded. Why do you need it? It's very easy to use `UIAlertController` in a usual manner. – OOPer Sep 13 '18 at 01:05
  • @OOPer this bookstore seemed very complete and resolved several dilemmas, but you're right in your analysis. For this reason I asked the suggestion of people with more learning than me. **I thought I had found a good Alert with a datepicker** – Zeus Tune Sep 13 '18 at 01:29
  • @ZeusTune, it is true that showing date picker in an alert view controller is difficult. It's because it is **not supported**. The library uses KVC to access non-public member, which may be treated as using private APIs, and has risk to be rejected by Apple. It also depends on the implementation details which you, app developers, should not rely on. Such implementation details may change in the future without any notification. If you want to show UIDatePicker modally, you can do it only with public APIs. If you are _practicing_ something, you should better learn how to use public APIs. – OOPer Sep 13 '18 at 01:49
  • @OOPer you're right, I'm going to look for public API documentation. Effectively, I'm practicing and it will be the best way to learn. I really like your comments – Zeus Tune Sep 13 '18 at 02:01

0 Answers0