当用户选择选项卡时,如何重置UITabBarController的选项卡(或选项卡控制器)?

时间:2017-04-28 10:32:30

标签: ios swift xcode uitabbarcontroller uitabbar

Swift:我有UITabBarController,有8个标签。当用户选择包含更多选项卡的任何选项卡时,我希望通过弹出到rootView控制器重置所选选项卡的内容?

怎么可以这样做?

我尝试使用以下方法重置导航控制器,它适用于底部可见的标签,但不适用于tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) 标签。

for(int count = 0; count< amount; count++){                             
           text =text + array[count] + "\n";
           try(  PrintWriter out = new PrintWriter( "nums.txt" )  ){
                out.println(text);
            }//end try
           catch (FileNotFoundException ex) { 
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE,null, ex);
            }//end catch
        }// end for

5 个答案:

答案 0 :(得分:4)

干净利落的方法:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    let index = tabBarController.selectedIndex
    if index == NSNotFound || index > 4 {
        tabBarController.moreNavigationController.popToRootViewController(animated: false)
        return
    }
    let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
    navController?.popToRootViewController(animated: false)
}

为您的UITabViewContoller创建一个自定义类,设置一个委托并将那段代码放在那里。

答案 1 :(得分:0)

你可以为你的tabBar创建一个UITabBarController类,并在那个类中从tabBar didSelect方法中选择你可以选择的任何选项卡popViewController

 class TabBarViewController: UITabBarController {
        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
            let navigationController1 =  self.viewControllers![0] as? UINavigationController
            navigationController1!.popViewController(animated: false)

            let navigationController2 =  self.viewControllers![1] as? UINavigationController
            navigationController2!.popToRootViewController(animated: false)


            let navigationController3 =  self.viewControllers![2] as? UINavigationController
            navigationController3!.popToRootViewController(animated: false)

            let navigationController4 =  self.viewControllers![3] as? UINavigationController
            navigationController4!.popToRootViewController(animated: false)

        }
}

答案 2 :(得分:0)

使用viewControllers的{​​{1}}属性。 viewControlers是UITabbarController的UIViewController数组。选择标签栏(控制器)时视图控制器的交换/移动位置。

使用tabbar(控制器)的委托方法来处理这些操作,例如:
Swift 3

UITabbarController

如果您可以访问已实施的tabbarController,也可以使用此委托方法。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    if var viewcontrollers = tabBarController.viewControllers {
        viewcontrollers.remove(at: tabBarController.selectedIndex)
        viewcontrollers.insert(viewController, at: 0)
        tabBarController.viewControllers = viewControllers
    }
}

答案 3 :(得分:0)

这对我有用。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
        return
    }

    navigationController.popToRootViewController(animated: false)
}

答案 4 :(得分:0)

要完全支持More,请创建如下的自定义类:

class Main_TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.customizableViewControllers = []  //remove Edit from More (OPTIONAL)
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if let index = tabBar.items?.firstIndex(of: item) {
            if let count = viewControllers?.count, count > 5, index == 4 {
                DispatchQueue.main.async {
                    self.moreNavigationController.popToRootViewController(animated: false)
                }
            } else if let vcs = viewControllers, let vc = vcs[index] as? UINavigationController {
                DispatchQueue.main.async {
                    vc.popToRootViewController(animated: false)
                }
            }
        }
    }

}