-1

I am trying to make an alert on my app, but it keeps giving me warning like the below and it's not appearing

Warning: Attempt to present <UIAlertController: 0x..> on <xyz.VC1: 0x..> whose view is not in the window hierarchy! 

the logic is like this :-

the IBAction in (VC1) calls a public function (X)

(X) the function do some operation and functions and based on it it's called the public function (Alert)

(Alert) the function should present an alert, but it gives me the previous warning.

NOTE: the alert works fine if I use it directly from the IBAction

present the alert :

func WAlert(){
  //  print("Wrong :("") // to be an alert
    let alert = UIAlertController(title: "S?", message: "Y", preferredStyle: UIAlertController.Style.alert)

    alert.addAction(UIAlertAction(title: "C", style: UIAlertAction.Style.default, handler: { _ in
        //Cancel Action
    }))
    alert.addAction(UIAlertAction(title: "out",
                                  style: UIAlertAction.Style.default,
                                  handler: {(_: UIAlertAction!) in
                                    //Sign out action
    }))
    present(alert, animated: true, completion: nil)

    //self.present(alert, animated: true, completion: nil)
    //VC1.present(alert, animated: true, completion: nil)
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
OneOfThem
  • 23
  • 6
  • Please add code of how you present the alert . – Teja Nandamuri Apr 26 '19 at 17:10
  • Could you show us the code used? Also, does calling this public function involve any async threading? Have you tried defining the function within VC1 and calling it that way? – GntlmnBndt Apr 26 '19 at 17:11
  • you can only present alert on a view controller that is visible to user. – Teja Nandamuri Apr 26 '19 at 17:17
  • it will work that way but I'm trying to avoid "spaghetti code" , I'm trying to make the code simple and easy to maintain and edit for further updates , that's why i create a separate file for "functions" "views" "alert" ...etc – OneOfThem Apr 26 '19 at 17:22
  • You are probably calling that too soon. You cannot present a controller unless the presenting controller has already appeared. That means you cannot present it from `viewDidLoad` or `viewWillAppear`. You can present a controller from `self` only after `viewDidAppear` has been called. – Sulthan Apr 26 '19 at 17:24
  • i'm facing the same issue , it worked for me only if the alert inside an IBAction – OneOfThem Apr 26 '19 at 18:09

1 Answers1

0

You probably need to have the function return the alert, rather than presenting it, and then present it (self.present) from VC1. As Teja Nandamuri mentions in comments, an alert must be presented from a visible UIViewController.

Revised based on comment: You can generate the alert in a separate function, like your WAlert, but still present it in VC1, even in the IBAction. For instance, in the action you would have:

let alert = WAlert()
self.present(alert, animated: true)

You would need to change WAlert as follows:

func WAlert() -> UIAlertController {
    let alert = UIAlertController(title: "S?", message: "Y", preferredStyle: UIAlertController.Style.alert)

    alert.addAction(UIAlertAction(title: "C", style: UIAlertAction.Style.default, handler: { _ in
        //Cancel Action
    }))
    alert.addAction(UIAlertAction(title: "out", style: UIAlertAction.Style.default, handler: {(_: UIAlertAction!) in
        //Sign out action
    }))
    return alert
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
GntlmnBndt
  • 249
  • 2
  • 6
  • i already tried to present the alert but it turned out that the alert only work(or my alert) within the IBAction – OneOfThem Apr 26 '19 at 17:33