如何在iOS应用程序(Swift)中单击选项卡栏项目时重定向到rootviewcontroller
我正在用Swift中的如何在iOS应用程序(Swift)中单击选项卡栏项目时重定向到rootviewcontroller,ios,swift,uitabbarcontroller,uitabbar,Ios,Swift,Uitabbarcontroller,Uitabbar,我正在用Swift中的uitabar和UINavigationViewController实现一个iOS应用程序。现在面对一个问题, 如果我选择第一个选项卡,我可以看到“A”ViewController,单击“A”的任何内容,我将重定向到“B”UINavigationViewController,现在如果我单击第二个选项卡,然后再次单击第一个选项卡,它将显示“B”NavigationViewController。应为,它应显示“A”ViewController。如何实现这一点?尝试实现didSe
uitabar
和UINavigationViewController
实现一个iOS应用程序。现在面对一个问题,
如果我选择第一个选项卡,我可以看到“A”
ViewController
,单击“A”的任何内容,我将重定向到“B”UINavigationViewController
,现在如果我单击第二个选项卡,然后再次单击第一个选项卡,它将显示“B”NavigationViewController
。应为,它应显示“A”ViewController
。如何实现这一点?尝试实现didSelectViewController
委托,然后在选择“一个ViewController”索引时重定向到根ViewController
@IBAction func itemB(sender: UIButton) {
// do something
self.tabBarController?.selectedIndex = 0
}
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
let index : Int = (tabBarController.viewControllers?.indexOf(viewController))!
if index == 0
{
let navigationController = viewController as? UINavigationController
navigationController?.popToRootViewControllerAnimated(true)
}
}
尝试实现
didSelectViewController
委托,然后在选择“一个ViewController”索引时重定向到根ViewController
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
let index : Int = (tabBarController.viewControllers?.indexOf(viewController))!
if index == 0
{
let navigationController = viewController as? UINavigationController
navigationController?.popToRootViewControllerAnimated(true)
}
}
在Swift 3.1中 将
uitabarcontrollerdelegate
添加到选项卡栏类:
class YourClass: UITabBarController, UITabBarControllerDelegate {
之后:
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
yourView .popToRootViewControllerAnimated(false)
}
在Swift 3.1中 将
uitabarcontrollerdelegate
添加到选项卡栏类:
class YourClass: UITabBarController, UITabBarControllerDelegate {
之后:
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
yourView .popToRootViewControllerAnimated(false)
}
你能解释清楚一点吗。我也有同样的问题,你能解释清楚一点吗。我也有同样的问题。