Patrick Kaim
Patrick Kaim

Reputation: 66

SWIFT: Navigate to UIViewControllers from single function with a variable

I'm trying to make a function for my navigation without having to type the exact same lines over and over again.

Trying to do so results into an error: "Use of undeclared type 'viewController'"

Am I doing this completely wrong, should I just place these 4 lines on every button just with different identifier and viewController?

func navigateTo(identifier:String, viewController:UIViewController) {
   let newVC = storyboard?.instantiateViewController(withIdentifier: identifier) as! viewController
   newVC.modalPresentationStyle = .fullScreen
   newVC.modalTransitionStyle = .flipHorizontal
   self.present(newVC, animated: true, completion: nil)
}

@IBAction func btnOneTapped(_ sender: Any) {
   navigateTo("myVC", MyViewController)
}

Upvotes: 0

Views: 62

Answers (3)

Patrick Kaim
Patrick Kaim

Reputation: 66

Thank you guys, I've got it working like so:

func navigateToVC(identifier:String, viewController:ViewController.Type) {
   let newVC = storyboard?.instantiateViewController(withIdentifier: identifier) as! ViewController
   newVC.modalPresentationStyle = .fullScreen
   newVC.modalTransitionStyle = .flipHorizontal
   self.present(newVC, animated: true, completion: nil)
}

@IBAction func btnOneTapped(_ sender:Any) {
   navigateToVC(identifier: "myVC", viewController: MyViewController.self)
}

Upvotes: 0

 Engineer
 Engineer

Reputation: 1

Make sure you provide correct identifier...below code works perfectly.

Your Error -> Trying to do so results into an error: "Use of undeclared type 'viewController'"

In this there is a typo mistake for ViewController i.e you are trying this - viewController

Also always try to do Optional-Binding instead to force-unwarpping it may cause crash in your app.

 func navigateTo(identifier:String, viewController:UIViewController) {
        if let newVC = storyboard?.instantiateViewController(withIdentifier: identifier) {
            newVC.modalPresentationStyle = .fullScreen
            newVC.modalTransitionStyle = .flipHorizontal
            self.present(newVC, animated: true, completion: nil)
        }
    }

    @IBAction func confirmBtn(_ sender: UIButton) {

        navigateTo(identifier: "myVC", viewController: MyViewController())

    }

Upvotes: 2

Frankenstein
Frankenstein

Reputation: 16341

You are passing an instance of UIViewController and trying to cast the UIViewController to it in the next line. Modify your function to take in ViewController.Type as parameter like this:

func navigateTo<ViewController: UIViewController>(identifier: String, viewController: ViewController.Type) {
    let newVC = storyboard?.instantiateViewController(withIdentifier: identifier) as! ViewController
    newVC.modalPresentationStyle = .fullScreen
    newVC.modalTransitionStyle = .flipHorizontal
    self.present(newVC, animated: true, completion: nil)
}

@IBAction func btnOneTapped(_ sender: Any) {
    navigateTo(identifier: "myVC", viewController: MyViewController.self)
}

Upvotes: 1

Related Questions