1

I was wondering if it is possible to push to a newViewController after pressing a button in the UiAlertController

my code looks like

 @objc func handleAcceptRequest() {
    let user = self.user
    let username = user?.name

    let alert = UIAlertController(title: "Are you Sure? ",message:" Would you like to accept \(username!) to complete your job?", preferredStyle: UIAlertController.Style.alert)

    let cancelButton = UIAlertAction(title: "Cancel", style: .default, handler: {(_ action: UIAlertAction) -> Void in
           print("you pressed cancel button")
       })

    let continueButton = UIAlertAction(title: "Continue", style: .default, handler: {(_ action: UIAlertAction) -> Void in

        let vc = viewController()
        let navController = UINavigationController(rootViewController: vc)

           print("you pressed Continue")

       })



    continueButton.setValue(GREEN_Theme, forKey: "titleTextColor")
    cancelButton.setValue(UIColor.red, forKey: "titleTextColor")
    alert.addAction(cancelButton)
    alert.addAction(continueButton)
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)

  }

I would only like to present the VC if the the Continue button is pressed but If I call the present

// self.present(navController, animated: true, completion: nil)

inside of the continueButton, I get the error Use of unresolved identifier 'present'

is it possible to push a newVC this way?

Zach Wilcox
  • 39
  • 1
  • 7
  • Your question doesn't provide much context where you're instantiating your `UIAlertController`. It looks like you are doing the instantiation outside of a `UIViewController` environment. Otherwise you'd get the `present(_:animated:completion)` method available. – nayem Oct 14 '19 at 03:17

1 Answers1

1

This is not right

// self.present(navController, animated: true, completion: nil)

It should be:

self.window?.rootViewController?. present(navController, animated: true, completion: nil)
jacob
  • 1,024
  • 9
  • 14
  • Cheers, happy coding – jacob Oct 14 '19 at 03:03
  • Can you explain why ___this is not right___ as you wrote in the beginning? – nayem Oct 14 '19 at 03:11
  • I guess that you call this method in your `AppDelegate`. And the `present` method is not available for `AppDelegate`, it's available for UIViewController, that why you need to use self.window.rootViewController (an instance of UIViewController) to present the alert @nayem – jacob Oct 14 '19 at 03:15
  • Well, I see. In that case (as you're guessing) you should have been explaining this right in your answer as well. – nayem Oct 14 '19 at 03:19
  • @nayem Sure, thanks for your suggestion!, will do next times. – jacob Oct 14 '19 at 03:20
  • @nayem this is actually created inside of a UIView not in app delegate. Thanks for all of the help! – Zach Wilcox Oct 14 '19 at 23:12
  • @Jacob thanks for your code, but I noticed that If I use this method, it removes the navigation bar at the top of the screen. is there a work around for this? – Zach Wilcox Oct 15 '19 at 00:07
  • Please check you ViewController (which is embedded in UINavigationController) and make sure that navigation bar is not hidden from this controller @ZachWilcox – jacob Oct 15 '19 at 03:11