programmatically push to a new viewController after pressing uiAlert button

100 Views Asked by At

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?

1

There are 1 best solutions below

8
jacob On BEST ANSWER

This is not right

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

It should be:

self.window?.rootViewController?. present(navController, animated: true, completion: nil)