Unable to set action for UITabbarItem Programmatically

我的未来我决定 提交于 2019-12-11 06:32:46

问题


i need to set action for my UITabbar, As when i click my tabbaritem it should go to an action which i am having in another viewcontroller.

but it is redirecting to UITableview

here is my code -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// create our table view controller that will display our store list    
FirstViewController *firstViewController = [[EzMasterViewController alloc] init];


// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[navController.tabBarItem setTitle:@"Home"];
[navController.tabBarItem setImage:[UIImage imageNamed:@"53-house.png"]];


SecondViewController *secondViewController = [[SecondViewController alloc] init];


// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[navController1.tabBarItem setTitle:@"Scan"];
[navController1.tabBarItem setImage:[UIImage imageNamed:@"195-barcode.png"]];

ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];


// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
[navController2.tabBarItem setTitle:@"Map"];
[navController2.tabBarItem setImage:[UIImage imageNamed:@"07-map-marker.png"]];

// add our view controllers to an array, which will retain them
NSArray *viewControllers = [NSArray arrayWithObjects:navController, navController1,navController2, nil];

// add our array of controllers to the tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllers];

// set the tab bar controller as our root view controller    
[self.window setRootViewController:tabBarController];

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;

}

By clicking on SecondViewController tabbaritem it should redirect to the action, where the action is in FirstViewController.


回答1:


You should consider implementing the UITabBarControllerDelegate's two methods:

– tabBarController:shouldSelectViewController:
– tabBarController:didSelectViewController:


来源:https://stackoverflow.com/questions/10926391/unable-to-set-action-for-uitabbaritem-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!