在 UITabController 中关闭视图控制器
Dismiss View Controller within UITabController
我的故事板上有以下堆栈
-> 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
,那只是从 class 到 class 的简单委托传递,但令人困惑的部分是中间 Tab 控制器。
您可以从 SettingViewController 发出自定义 NSNotification,它将由 MyCoursesViewController 处理并执行您需要的任何操作。
这种方法更正确,因为在开发过程中事情可能会变得更加复杂并且您的层次结构可能会发生变化。通过使用 NSNotification 可以降低代码的耦合度,这是一件好事
试试这个:
self.tabBarController?.navigationController?.popViewControllerAnimated(true)
我认为要从 MyCoursesViewController
转到您的 UITabController
,您正在使用 pushViewController 或 segue 为您做这件事,所以要返回,您必须使用 popViewControllerAnimated
。
dismissViewControllerAnimated
用于消除使用 presentViewController
显示的 viewcontroller
编辑:
因此,如果您有两个 UINavigationController,我认为此代码适合您:
self.navigationController?.parentViewController?.navigationController!.popViewControllerAnimated(true)
根据您的描述,MyCoursesViewController
听起来像是您的 NavigationController
的 rootViewController
。
您将 CourseInfoUITabController
推入堆栈,而您的 SettingViewController
实际上嵌入了 UINavigationController
因此您可以这样做:
self.navigationController?.tabBarController?.navigationController?.popViewControllerAnimated(true)
在我看来,如果我正确理解您的视图层次结构,它应该只是:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let rootNavigationController = appDelegate.window?.rootViewController as! UINavigationController
rootNavigationController.popToRootViewControllerAnimated(true)
我的故事板上有以下堆栈
-> 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
,那只是从 class 到 class 的简单委托传递,但令人困惑的部分是中间 Tab 控制器。
您可以从 SettingViewController 发出自定义 NSNotification,它将由 MyCoursesViewController 处理并执行您需要的任何操作。
这种方法更正确,因为在开发过程中事情可能会变得更加复杂并且您的层次结构可能会发生变化。通过使用 NSNotification 可以降低代码的耦合度,这是一件好事
试试这个:
self.tabBarController?.navigationController?.popViewControllerAnimated(true)
我认为要从 MyCoursesViewController
转到您的 UITabController
,您正在使用 pushViewController 或 segue 为您做这件事,所以要返回,您必须使用 popViewControllerAnimated
。
dismissViewControllerAnimated
用于消除使用 presentViewController
编辑:
因此,如果您有两个 UINavigationController,我认为此代码适合您:
self.navigationController?.parentViewController?.navigationController!.popViewControllerAnimated(true)
根据您的描述,MyCoursesViewController
听起来像是您的 NavigationController
的 rootViewController
。
您将 CourseInfoUITabController
推入堆栈,而您的 SettingViewController
实际上嵌入了 UINavigationController
因此您可以这样做:
self.navigationController?.tabBarController?.navigationController?.popViewControllerAnimated(true)
在我看来,如果我正确理解您的视图层次结构,它应该只是:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let rootNavigationController = appDelegate.window?.rootViewController as! UINavigationController
rootNavigationController.popToRootViewControllerAnimated(true)