如何在另一个导航控制器的导航控制器中显示根vc上方的视图控制器

时间:2017-09-08 03:13:02

标签: ios swift swift3 uiviewcontroller uinavigationcontroller

我正在为用户注册系统,当他们注册时我希望他们通过教程,本教程已经以几种不同的方式呈现在HomeVC中。我不想创建教程的克隆并将其粘贴到注册过程中,我认为这是一种简单的方法。寻找最佳实践:)

所以注册后这就是我正在做的事情

 //0. Currently in "Signup" Nav stack
 //1. init VCs for new nav stack
 //2. push tutorial onto HomeVC nav w/o animation
 //3. present stack with HomeVC below Tutorial, but tutorial is what animates in since in on top?


//@1. 
let HomeVC = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "HomeViewNavigationController")
let TutorialVC = UIStoryboard(name: "Tutorial", bundle: nil).instantiateViewController(withIdentifier: "TutorialViewController")

//@2.
HomeVC.navigationController?.pushViewController(TutorialVC, animated: false);

//@ 3.
self.present(HomeVC, animated: true)

问题在于“HomeVC”将首先闪现,然后它将为“TutorialVC”制作动画。

很确定我只是构建这个非常糟糕的, 提前致谢

3 个答案:

答案 0 :(得分:1)

  

导航控制器对象使用导航堆栈管理当前显示的屏幕,导航堆栈由视图控制器数组表示

     

数组中的最后一个视图控制器是当前正在显示的视图控制器。

因此,您可以创建array viewcontroller并将其分配到viewControllers <{1}}的{​​{1}}属性

  
    

NavigationController

  

您不需要将var viewControllers: [UIViewController]推送到TutorialVC

了解更多信息https://developer.apple.com/documentation/uikit/uinavigationcontroller

答案 1 :(得分:1)

使用navigationController的viewControllers属性创建所需的堆栈。 使用以下代码

let HomeVC = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "HomeViewNavigationController")
let TutorialVC = UIStoryboard(name: "Tutorial", bundle: nil).instantiateViewController(withIdentifier: "TutorialViewController")
//@2.
let navigationController = UINavigationController(rootViewController: HomeVC);

//@3.
navigationController.viewControllers = [HomeVC,TutorialVC]

//@ 4.
self.present(navigationController, animated: true)

希望这会有所帮助

答案 2 :(得分:1)

我认为最好的方法是使用MainViewController和Notifications,并执行以下操作:

MainViewController:

//init VCs
override func viewDidLoad() {
    super.viewDidLoad()
    self.initTutorialViewController()
    self.initSignupViewController()
    self.initHomeViewController()

    //check if for sign up
    if isForSignUp {
      self.addChildViewController(self.signupViewController!)
      self.view.addSubview((self.signupViewController?.view)!)
    } else {
      self.addChildViewController(self.homeViewController!)
      self.view.addSubview((self.homeViewController?.view)!)
    }
    //other codes here
}

然后你也应该在MainViewController中使用这些辅助函数:

func transition(fromViewController:UIViewController, toViewController:UIViewController) {
    fromViewController.willMove(toParentViewController: nil)
    self.addChildViewController(toViewController)

    self.transition(from: fromViewController, to: toViewController, duration: 0.25, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {() -> (Void) in
    }, completion: {(success) -> (Void) in
        fromViewController.removeFromParentViewController()
        toViewController.didMove(toParentViewController: self)
    })
}

//notification for signing up
func didSignup(notification:Notification) {
  self.transition(fromViewController: self.signUpViewController!, 
  toViewController: self.tutorialViewController!)
}

func didFinishTutorial(notification: Notification) {
  self.transition(fromViewController: self.tutorialViewController!, 
  toViewController: self.homeViewController!)
}

//if you don't need tutorial to be transitioned to
//add other handlers here.

就是这样,我不确定这是否是最好的做法,但如果我需要转到LoginPage或直接转到HomeVC,如果sessionToken仍处于活动状态,这也是我处理转换的方式。希望这有帮助!