今天在做项目的时候遇到了一个问题:怎么判断进入一个(view)界面是tabbar左右切换显现的还由详情界面推出显示的,因为不同的显示view的方式,我要进行不同的处理。所以,就研究了一下UITabBarDelegate & UITabBarControllerDelegate
UITabBarDelegate
TabBarDelegate 一共有五个代理方法,而且代理方法必须写在UITabBarController的控制器里面,不然代理方法不会执行。直接在UITabBarController控制器里面写<UITabBarDelegate>,他就可以执行代理方法
代理方法
[目前感觉就是第一个有用,其他的不知道干啥]
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(@"选中了某个item");
}
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items{
NSLog(@"将要开始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items{
NSLog(@"已经开始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed {
NSLog(@"将要结束自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed{
NSLog(@"将要结束自定制item");
}
UITabBarControllerDelegate
//设置控制器数组
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//设置控制器数组 动画
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//选中的控制器
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController;
//选中索引值
@property(nonatomic) NSUInteger selectedIndex;
//当item超过五个时 就会有一个更多
@property(nonatomic, readonly) UINavigationController *moreNavigationController;
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers;
//tab条
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);
//委托
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;
@end
1、在Application的中编码,平时项目要使用继承一个于UITabBarController的控制器里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//初始化一个tabBar控制器
UITabBarController *tb = [[UITabBarController alloc]init];
//设置UIWindow的rootViewController为UITabBarController
self.window.rootViewController = tb;
//创建相应的子控制器
UIViewController *vc1 = [[UIViewController alloc]init];
vc1.view.backgroundColor = [UIColor greenColor];
vc1.tabBarItem.title = @"首页";
vc1.tabBarItem.image = [[UIImage imageNamed:@"Home_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"Home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.view.backgroundColor = [UIColor blueColor];
vc2.tabBarItem.title = @"分类";
vc2.tabBarItem.image = [[UIImage imageNamed:@"List_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"List_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0],NSForegroundColorAttributeName, [UIFont systemFontOfSize:10.0],NSFontAttributeName,nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255.0/255 green:73.0/255 blue:87.0/255 alpha:1.0],NSForegroundColorAttributeName, [UIFont systemFontOfSize:10.0],NSFontAttributeName,nil] forState:UIControlStateSelected];
//把子控制器添加到UITabBarController
//[tb addChildViewController:c1];
//[tb addChildViewController:c2];
//或者
tb.viewControllers = @[vc1,vc2];
[self.window makeKeyAndVisible];
return YES;
}
2、UITabBarControllerDelegate委托内容
1、视图将要切换时调用,viewController为将要显示的控制器,如果返回的值为NO,则无法点击其它分栏了(viewController指代将要显示的控制器)
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSLog(@"被选中的控制器将要显示的按钮");
//return NO;不能显示选中的控制器
return YES;
}
2、视图已经切换后调用,viewController 是已经显示的控制器
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"视图显示后调用");
}
3、将要开始自定义item的顺序
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{
NSLog(@"将要开始自定义item时调用");
NSLog(@"%@",viewControllers);
}
4、将要结束自定义item的顺序
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
NSLog(@"将要结束自定义item时调用");
NSLog(@"%@",viewControllers);
}
网友评论