简体   繁体   English

在SWIFT中的不同视图之间切换

[英]Switch between different Views in SWIFT

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. 我在堆栈溢出社区中阅读了很多关于在不同的ViewController之间进行切换的问题/答案,但是我找不到针对我的特定问题的正确答案。

  1. I'm using a NavigationViewController to "connect" all my different viewControllers (ViewController / TableViewController / CollectionViewController) 我正在使用NavigationViewController来“连接”所有不同的viewController(ViewController / TableViewController / CollectionViewController)

  2. I have got some classes which aren't depending on a ViewController class. 我有一些不依赖于ViewController类的类。 For example to to calculate anything in my "calculate" class. 例如要计算我的“计算”类中的任何内容。

I'm searching for an easy solution, to switch between all my ViewControllers. 我正在寻找一个简单的解决方案,以便在所有ViewController之间切换。 And I would like to decide in my "calculate" class eg to go one step back in my Navigation ViewController. 我想在“计算”类中做出决定,例如,在导航ViewController中退后一步。

In the moment I use this: 目前,我使用这个:

  1. Example to switch between two different ViewControllers: 在两个不同的ViewController之间切换的示例:

     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. 问题:此示例仅在ViewController类中起作用,导航栏消失。

  1. Example to go one step back in my NavigationViewController: 回到我的NavigationViewController的示例:

     self.navigationController?.popViewControllerAnimated(true) 

PROBLEM: This example only works in a ViewController class. 问题:此示例仅在ViewController类中有效。

EDIT 1: 编辑1:

I found a nice solution to call all my viewControllers with this part of code: 我找到了一个不错的解决方案,可以用这部分代码来调用所有的viewController:

    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? 如何在非ViewController类中调用此代码?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM