简体   繁体   English

以编程方式将视图控制器添加到导航控制器,然后推送另一个视图控制器

[英]Add viewcontroller to navigation controller programmatically then push another view controller

I want to append couple of viewcontrollers in array of viewcontroller of a navigation controller, then want push third viewcontroller of the same navigation controller.我想在导航控制器的视图控制器数组中附加几个视图控制器,然后想要推送同一导航控制器的第三个视图控制器。 My code is as follows我的代码如下

let navigationController = getCurrentNavController()

let listVC = UIStoryboard.loadListViewController()
navigationController.viewControllers.append(listVC)
                
let detailVC = UIStoryboard.loadDetailsViewController()
navigationController.viewControllers.append(detailVC)
                
let thirdVC = UIStoryboard.loadThird()
navigationController.pushViewController(thirdVC, animated: true)

The thirdVC is push on the navigation controller, the problem is when i pop thirdVC, I don't find detailVC or listVC.第三个VC是在导航控制器上推送,问题是当我弹出第三个VC时,我没有找到detailVC或listVC。 Is this possible?这可能吗? if yes please help.如果是,请帮忙。

Don't edit the viewControllers array directly.不要直接编辑viewControllers数组。 That is why the animated parameter exists.这就是animated参数存在的原因。

A solution may be to add the viewControllers in between after 0.25 seconds (the duration of the push animation) by doing:一个解决方案可能是通过执行以下操作在 0.25 秒(推送动画的持续时间)之后添加 viewControllers:

let navigationController = getCurrentNavController()
var viewControllers = navigationController.viewControllers

let listVC = UIStoryboard.loadListViewController()
viewControllers.append(listVC)
                
let detailVC = UIStoryboard.loadDetailsViewController()
viewControllers.append(detailVC)
                
let thirdVC = UIStoryboard.loadThird()
viewControllers.append(thirdVC)
navigationController.pushViewController(thirdVC, animated: true)

DispatchQueue.main.asyncAfter(now() + 0.25) {
    navigationController.setViewControllers(viewControllers, animated: false)
}

As mentioned, you do not want to modify the navigation controller's viewControllers directly.如前所述,您不想直接修改导航控制器的viewControllers

But, you also don't need to do any explicit "pushing":但是,您也不需要进行任何明确的“推送”:

    let navigationController = getCurrentNavController()

    let listVC = UIStoryboard.loadListViewController()
    let detailVC = UIStoryboard.loadDetailsViewController()
    let thirdVC = UIStoryboard.loadThird()

    var vcs = navigationController.viewControllers

    vcs.append(listVC)
    vcs.append(detailVC)
    vcs.append(thirdVC)

    navigationController.setViewControllers(viewControllers, animated: true)

This will animate thirdVC in from the current view controller, just as if you had pushed it, but it will also insert listVC and detailVC into the navigation controller's stack so the "Back" button will take you through detailVC and listVC .这将从当前视图控制器中为thirdVC设置动画,就像您已经推送它一样,但它还会将listVCdetailVC插入导航控制器的堆栈中,因此“后退”按钮将带您浏览detailVClistVC

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

相关问题 导航 Controller 未加载(推送,呈现)另一个视图控制器 - Navigation Controller is not loading ( push , present ) another viewController 以编程方式推送至导航堆栈中的视图控制器[2],然后展开至[1] - Programmatically push to view controller [2] in a navigation stack, and unwind to [1] 如何以编程方式将下一个视图控制器推入导航控制器? - How can I push the next view controller on a navigation controller programmatically? 导航控制器推送视图控制器 - Navigation Controller Push View Controller 如何在代码中以编程方式添加导航控制器而不是作为初始视图控制器 - How to add a navigation controller programmatically in code but not as initial view controller 导航控制器show / push viewcontroller swift - Navigation Controller show/push viewcontroller swift 具有到另一个视图控制器的tableview的ViewController - ViewController with a tableview to another view controller 以编程方式创建和推送视图控制器 - Programmatically Create and Push View Controller 使用导航控制器推送新的视图控制器 - Push a new view controller with navigation controller 具有交互式过渡的导航控制器中的推式视图控制器 - Push view controller in navigation controller with interactive transition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM