在UITabController中关闭View Controller

时间:2016-07-03 15:15:12

标签: ios swift uiviewcontroller uitabbar

我的故事板上有以下堆栈

-> Navigation Controller 
 -> MyCoursesViewController (does a prepareForSegue to next 'view')
  -> Course Info UITabController (I select Setting Tab)
   -> Setting Navigation Controller
    -> SettingViewController

当我在设置标签:SettingViewController中时,我有一个UIButton,上面写着'退出课程',我想让它回到MyCoursesViewController。如何弹出以访问此视图控制器?

我尝试了下面的事情:

self.tabBarController?.navigationController?.dismissViewControllerAnimated(true, completion: nil)
self.navigationController?.tabBarController?.dismissViewControllerAnimated(true, completion: nil)
self.dismissViewControllerAnimated(true, completion: nil)
self.tabBarController?.navigationController?.popViewControllerAnimated(true)
self.navigationController?.popViewControllerAnimated(true)

但无济于事。

有没有办法简单地解雇或者我需要从MyCoursesViewController实现一个委托来通过UITabController传递给SettingViewController?我不确定如何将委托传递给MyCourses -> Course Info Tab -> Setting。如果它是MyCourses -> Setting,那只是一个简单的委托从类到类的传递,但令人困惑的部分是中间的Tab控制器。

4 个答案:

答案 0 :(得分:2)

试试这个:

self.tabBarController?.navigationController?.popViewControllerAnimated(true)

我认为从MyCoursesViewController转到你的UITabController你正在使用pushViewController或者是segue为你做的,所以要回去你必须使用popViewControllerAnimated

dismissViewControllerAnimated用于解除使用presentViewController

显示的viewcontroller

编辑:

所以如果你有两个UINavigationController,我认为这段代码对你有用:

self.navigationController?.parentViewController?.navigationController!.popViewControllerAnimated(true)

答案 1 :(得分:1)

您可以从SettingViewController发出自定义NSNotification,它将由MyCoursesViewController处理并执行您需要的任何操作。

这种方法更为正确,因为在开发期间,事情可能会变得更加复杂,您的层次结构可能会发生变通过使用NSNotification,您可以减少耦合代码的部分,这是一件好事

答案 2 :(得分:1)

根据您的说明,MyCoursesViewController听起来像rootViewController的{​​{1}}。

您已将NavigationController推送到您的筹码堆中,CourseInfoUITabController实际上已嵌入SettingViewController,因此您可以执行此操作:

UINavigationController

答案 3 :(得分:1)

在我看来,如果我理解你的视图层次结构,它应该只是:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let rootNavigationController = appDelegate.window?.rootViewController as! UINavigationController
rootNavigationController.popToRootViewControllerAnimated(true)
相关问题