且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS-点击TabBarItem,检查变量以显示相应的ViewController

更新时间:2023-02-17 14:38:53

我了解的是,单击或轻按Tab时需要获取通知.

在AppDelegate的 didFinishLaunchingWithOptions
  UITabBarController * tabBar =(UITabBarController *)self.window.rootViewController;[tabBar setDelegate:self]; 

现在,在 didSelectViewController 中,您可以编写关于显示内容的逻辑或条件.

 -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{//在这里写你的逻辑tabBarController.selectedViewController = yourNewController;} 

Please help me find solution for case:

I have UITabBarController in storyboard. When I tap on a TabBarItem (index 1), I should check variable (int)'ShowVC' to show corresponding view controller. E.g:

switch (ShowVC) {
            case 1:
                showViewController1;
                break;
            case 2:
                showViewController2;
                break;
            case 3:
                showViewController3;
                break;
            default:
                break;
        }

So, what is solution for it?

Where can I add check method to show view controller when tapped TabBarItem?

What I understand is you need to get Notification when your Tab is clicked or Tapped.

in AppDelegate's didFinishLaunchingWithOptions

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setDelegate:self];

Now in didSelectViewController you can write your logic or condition about what to display.

- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   //Write your logic here
   tabBarController.selectedViewController = yourNewController;
}