开发过程中遇到UITabBarController有5个tabBar,其他的tabBar都是正常的添加UINavigationController,但是中间的tabBar需要点击跳转。如果用自定义方式能实现,但是如果坚持想用系统自带的UITabBarController呢?其实方法也有许多,不过个人觉得用通知的方式是最清晰的。
首先,让AppDelegate实现代理UITabBarControllerDelegate,目的是监听用户所选择的tabBar,接着实现代理方法,具体代码如下:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0) {
if (viewController == [tabBarController.viewControllers objectAtIndex:2]) { //中间的tabBar的index为2
//将跳转标志位置为true,同时发送通知实现跳转
[OverallInfo sharedOverallInfo].isPushFromTab = @"true"; //这里设置了标志位是为了防止无限跳转
[[NSNotificationCenter defaultCenter] postNotificationName:@"ShouldPush"object:nil];
return NO; //点击中间按钮不允许是选中状态,返回后仍然是点击跳转前的