如何在TabBarController中重置UIViewController

时间:2018-12-28 16:43:17

标签: ios swift uiviewcontroller uinavigationcontroller

我看到了几个类似于我的问题,但到目前为止,没有一个答案对我有用。

我有一个TabBarController连接到3个NavigationControllers。每个NavigationController都有一个自定义的ViewController。请参见下面的屏幕截图。 这些ViewController之一具有TableView。我有一个取消按钮,可以通过在IBAction中选择另一个selectedIndex来退出此视图,该操作位于此ViewController的自定义类中。用户可以在每个单元格中输入文本字段。 但是,我想使用此取消按钮来重置ViewController,以便将TableView和控制器中的所有其他内容重置为它们的原始状态。我该怎么做?

enter image description here

@IBAction func cancel(_ sender: Any) {
        let index = (self.tabBarController as! MainTabBarController).previouslySelectedIndex!
        animateToTab(tabBarController: self.tabBarController!, to: self.tabBarController!.viewControllers![index])
        self.tabBarController?.selectedIndex = index
    }

我看到答案说在我的NavigationController上使用“ popToRootViewController”或“ popViewController”,但是这些都不起作用,可能是因为每个NavigationController中只有一个ViewController。

1 个答案:

答案 0 :(得分:1)

您可以尝试(将故事板标识符设置为该导航的rootVC之后)

let vc = storyboard!.instantiateViewController(withIdentifier:"CancelVC") as! CancelVC  
self.navigationController?.setViewControllers([vc],animated:true)

重新启动当前的VC

相关问题