Switch between different Views in SWIFT

2019-06-10 19:36发布

I read a lot of questions / answers in the stack overflow community about switching between different ViewControllers but I couldn't find the correct answer for my specific problem.

  1. I'm using a NavigationViewController to "connect" all my different viewControllers (ViewController / TableViewController / CollectionViewController)

  2. I have got some classes which aren't depending on a ViewController class. For example to to calculate anything in my "calculate" class.

I'm searching for an easy solution, to switch between all my ViewControllers. And I would like to decide in my "calculate" class e.g. to go one step back in my Navigation ViewController.

In the moment I use this:

  1. Example to switch between two different ViewControllers:

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView
    self.presentViewController(vc, animated: true, completion: nil)
    

PROBLEM: This example only works in a ViewController class and the navigation bar disappears.

  1. Example to go one step back in my NavigationViewController:

    self.navigationController?.popViewControllerAnimated(true)
    

PROBLEM: This example only works in a ViewController class.

EDIT 1:

I found a nice solution to call all my viewControllers with this part of code:

    let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
    self.navigationController?.popToViewController(switchViewController, animated: true)

How can I call this code in a non ViewController class?

1条回答
欢心
2楼-- · 2019-06-10 20:06

Push the new view controller onto your existing navigation stack, rather than presenting it modally:

let vc = self.storyboard!.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView
self.navigationController!.pushViewController(vc, animated: true)
查看更多
登录 后发表回答