控制器view的加载
生命周期
程序启动完成时
// 程序启动完成的时候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1.创建窗口,注意窗口必须要有尺寸,尺寸跟屏幕一样大的尺寸,窗口不要被释放
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor redColor];
// 2.创建窗口的跟控制器
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor yellowColor];
[vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
// 如果设置窗口的跟控制器,默认就会把控制器的view添加到窗口上
// 设置窗口的跟控制器,默认就有旋转功能
self.window.rootViewController = vc;
// [self.window addSubview:vc.view];
// 3.显示窗口
[self.window makeKeyAndVisible];
return YES;
}
代码加载storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建窗口的跟控制器
// 加载storyboard
// storyboard文件名,不需要带后缀
// nil: [NSBundle mainBundle]
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// 通过storyboard创建控制器
// instantiateInitialViewController:加载箭头指向的控制器
// UIViewController *vc = [storyboard instantiateInitialViewController];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"green"];
NSLog(@"%@",[vc class]);
self.window.rootViewController = vc;
// 显示窗口
[self.window makeKeyAndVisible];
return YES;
}
代码加载xib
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 通过xib创建控制器
ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
view的懒加载
创建UIViewController控制器,控制器的view并没有创建
控制器的view懒加载:第一次使用的时候才会去加载,并不是创建UIViewController控制器的时候去加载
navigation
// 控制器的view加载完成的时候调用
// 通常这个方法只会调用一次
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 设置导航条的内容:navigationItem
// 中间的文字标题
// self.navigationController.navigationItem.title = @"第一个控制器";
// self.navigationItem.title = @"第一个控制器";
self.title = @"第一个控制器";
// 中间的view
// self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
// 左边按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleDone target:self action:@selector(btnClick)];
// 导航条右边按钮
// 在iOS7之后默认会把导航条上面的按钮渲染成蓝色
UIImage *image = [UIImage imageNamed:@"navigationbar_friendsearch"];
// 通过代码告诉苹果不要渲染图片
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:nil action:nil];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
// 导航条上面的内容位置不能由开发者决定,开发者只能控制尺寸
// btn.frame = CGRectMake(2000, 3000, 30, 30);
// 控件的尺寸由图片决定
// 仅仅是设置尺寸
[btn sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
/*
只要以后看到Item,一般都是苹果提供的模型。
UINavigationItem:控制导航条的内容
UIBarButtonItem:控制导航条上面按钮的内容
*/
tarbar
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 设置窗口的跟控制器
UITabBarController *tabBarVc = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarVc;
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
// 添加子控制器
[tabBarVc addChildViewController:vc];
// 设置按钮上面的内容
vc.tabBarItem.title = @"消息";
vc.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"];
vc.tabBarItem.badgeValue = @"1000";
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor greenColor];
// 添加子控制器
[tabBarVc addChildViewController:vc1];
vc1.tabBarItem.title = @"联系人";
vc1.tabBarItem.badgeValue = @"10";
// 显示窗口
[self.window makeKeyAndVisible];
return YES;
}
满地打滚卖萌求赞,如果本文帮助到你,轻点下方的红心,给作者君增加更新的动力。