iOS开发之点击tabBarItem进行Push一个页面
使用场景:
实现底部Tabbar两个TabBarItem,点击第二个跳转push到个人信息页面:
实现步骤:
-
首先设置UITabBarController的代理为appdelegate如下:myTabBar.delegate = self;
-
然后在interface后面写上代理<UITabBarControllerDelegate>
-
实现代理里面的- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController方法即可
代码如下:
#pragma mark - 跳转对应的页面 -(void)jumpToMainVC:(NSString *)messageBadgeNumStr { self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.delegate = self; HomeViewController *homeVC = [[HomeViewController alloc] init]; self.tabBarController.viewControllers = @[[self navigationControllerWithFirstViewController:homeVC], [UIViewController new]]; NSArray *titles = @[@"首页", @"我的"]; NSArray *images = @[@"home_tab_home", @"home_tab_mine"]; [self.tabBarController.tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem *item, NSUInteger idx, BOOL *stop) { [item setTitle:titles[idx]]; [item setImage:[[UIImage imageNamed:images[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; [item setSelectedImage:[[UIImage imageNamed:[images[idx] stringByAppendingString:@"pre"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; CGFloat verticalOffSet = 2; UIEdgeInsets edgeInsets = item.imageInsets; edgeInsets.top = -verticalOffSet; edgeInsets.bottom = verticalOffSet; item.imageInsets = edgeInsets; [item setTitlePositionAdjustment:UIOffsetMake(0, -verticalOffSet)]; item.tag = idx; }]; self.window.rootViewController = self.tabBarController; }
实现跳转代理:
#pragma mark - UITabBarControllerDelegate - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { if (viewController.tabBarItem.tag == 1) { UserInfoViewController *userInfoVC = [[UserInfoViewController alloc] init]; UINavigationController *nav = self.tabBarController.selectedViewController; [nav pushViewController:userInfoVC animated:YES]; return NO; } return YES; }