//创建视图控制器
for (int i=0; i<5; i++) {
UIViewController *viewCtrl = [[UIViewController alloc] init];
[array addObject:viewCtrl];
viewCtrl.title = [NSString stringWithFormat:@"第%d控制器",i];
viewCtrl.view.backgroundColor = [UIColor colorWithRed:arc4random()%10*0.1 green:arc4random()%10*0.1 blue:arc4random()%10*0.1 alpha:1];
}
UITabBarController *tabbarCtrl = [[UITabBarController alloc] init];
tabbarCtrl.viewControllers = array;
//将当前对象作为tabbarCtrl的代理对象
tabbarCtrl.delegate = self;
_window.rootViewController = tabbarCtrl;
return YES;
}
//视图控制器将要切换的时候调用
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog(@"将要切换:%@",viewController);
return YES;
}
//视图控制器已经切换的时候调用
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
//取得选中的下标,可以通过修改selected切换视图控制器
NSInteger index = tabBarController.selectedIndex;
//取得标签控制器管理的视图控制器
NSArray *viewCtrl = tabBarController.viewControllers;
NSLog(@"viewCtrl:%@",viewCtrl);
NSLog(@"已经切换:%@,选中的下标:%ld",viewController,index);