Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS应用程序(Swift)中单击选项卡栏项目时重定向到rootviewcontroller_Ios_Swift_Uitabbarcontroller_Uitabbar - Fatal编程技术网

如何在iOS应用程序(Swift)中单击选项卡栏项目时重定向到rootviewcontroller

如何在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

我正在用Swift中的
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) 

}

你能解释清楚一点吗。我也有同样的问题,你能解释清楚一点吗。我也有同样的问题。