UIBarItem: 1.UIBarButtonItem;2.UITabBarItem
UIView: 1.UINavigationBar、UIToolBar、UITabBar
UIResponder: 1.UIViewController、UITabBarController、UINavigationController
UITabBarController结构:UITabBar、Custom content、Tab bar controller View;UITabBar 控制多个Tab,每个Tab的对象是UIViewController,每个Tab是UITabBarItem的对象
UINavigationController的层次结构:Navigation bar、Navigation toolbar、Custom content
QYTabBarViewController如何控制UIViewController
方法1
//创建3个viewController数组
-(void)bindViewController{
UIViewController *firstVC = [[UIViewControlleralloc] init];
firstVC.view.backgroundColor = [UIColorredColor];
UIViewController *secondVC = [[UIViewControlleralloc] init];
secondVC.view.backgroundColor = [UIColorgreenColor];
UIViewController *thirdVC = [[UIViewControlleralloc] init];
thirdVC.view.backgroundColor = [UIColorblueColor];
UIViewController *fourthVC = [[UIViewControlleralloc] init];
fourthVC.view.backgroundColor = [UIColororangeColor];
NSArray *viewControllers = [NSArrayarrayWithObjects:firstVC,secondVC,thirdVC, fourthVC,nil];
self.viewControllers = viewControllers;
}
//uiButton添加监听事件
UIButton *barItem = [UIButtonbuttonWithType:UIButtonTypeCustom];
//self 是 要调用action 的对象
[barItem addTarget:selfaction:@selector(changeViewController:)forControlEvents:UIControlEventTouchUpInside];
//根据tag值 在viewControllers 取出 一个视图控制器
-(void)changeViewController:(UIButton *)sender{
self.selectedIndex = sender.tag -100;
}
//_window 的根视图控制器 是 tabBarController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];
window.hidden =NO;
_window = window;
QYFirstViewController *firstVC = [[QYFirstViewControlleralloc]init];
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:firstVC];
QYSecondViewController *secondVC = [[QYSecondViewControlleralloc] init];
QYThirdViewController *thirdVC = [[QYThirdViewControlleralloc] init];
UITabBarController *tabBarController = [[UITabBarControlleralloc] init];
NSArray *vcs = [NSArrayarrayWithObjects:nav,secondVC,thirdVC, nil];
tabBarController.viewControllers = vcs;
_window.rootViewController = tabBarController;
//点击btn后,self将调用vcVC视图控制器
QYFirstViewController.m
- (void)viewDidLoad {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
[btn addTarget:selfaction:@selector(next:)forControlEvents:UIControlEventTouchUpInside];
}
-(void)next:(UIButton *)sender{
ViewController *vcVC = [[ViewControlleralloc] init];
[self.navigationControllerpushViewController:vcVC animated:YES];
}
@implementation QYFirstViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//创建一个tabBarItem
UITabBarItem *tabBarItem = [[UITabBarItemalloc] initWithTabBarSystemItem:UITabBarSystemItemTopRatedtag:1];
//把tabBarItem设置成当前视图控制器在tabBarController里显示的tab
self.tabBarItem = tabBarItem;
//设置徽章
// self.tabBarItem.badgeValue = @"10";
}
return self;
}
@end