1

I am transitioning between views programatically using the code below and it gets repeated quite a lot so I wanted to create a global function but I can't seem to be able to get the hang of it.

The code works when called inside a ViewController class, so I suppose the problem is that my function doesn't know which VC do I want to call navigationController.pushViewController on, but I don't know how to reference the VC either as an argument passed to the function, or better yet with something like .self to take the current VC class the function is called in.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ExamplesControllerVC")
self.navigationController?.pushViewController(vc, animated: true)

The error I get if I try to run that as a function in a separate file is:

Use of unresolved identifier 'navigationController'; did you mean 'UINavigationController'?

So the function I'd like to create and call is something like:

showVC("ExamplesControllerVC")

Any ideas?

0

2 Answers 2

2

Do you want to do something like this?:

extension UIViewController {
    func presentView(withIdentifier: String) {
        if let newVC = self.storyboard?.instantiateViewController(withIdentifier: withIdentifier) {
        self.present(newVC, animated: true, completion: nil)
        }
    }
}

You can call it this way:

self.presentView(withIdentifier: "yourIdentifier")
Sign up to request clarification or add additional context in comments.

2 Comments

That’s really clever, I didn’t think to use an extension. Thanks for explaining.
This extension is ok, but there's the one problem, it will only work if you use only one storyboard. You can add SB name parameter in the method name, and instantiate SB let sb: UIStoryboard = UIStoryboard(name: <<SB NAME PARAMETER>>, bundle: nil) after that, just replace self.storyboard with sb and you don't need that if let anymore
2

Whatever function this code is in needs to be updated to take a parameter of type UIViewController.

func showMain(on vc: UIViewController) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ExamplesControllerVC")
    vc.navigationController?.pushViewController(vc, animated: true)
}

Now you can call this as:

showMain(on: someViewController)

Or add this function to an extension on UIViewController then your use of self works just fine.

extension UIViewController {
    func showMain() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "ExamplesControllerVC")
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.