1.概念
视图控制器用来管理视图的加载,卸载,横屏竖屏显示等操作。
每一个界面都应该由一个控制器来管理显示
UIViewController是所有视图控制器的父类
视图控制器的根视图创建
//创建视图控制器
RootViewController *rootViewController = [[RootViewController alloc] init];
NSLog(@"开始创建视图");
//根视图控制器
//然后调用RootviewController中的方法
self.window.rootViewController = rootViewController;
//以前用这种方式
// [self.window addSubview:rootViewController.view];
NSLog(@"创建视图完成");
RootViewController *rootViewController = [[RootViewController alloc] init];
NSLog(@"开始创建视图");
//根视图控制器
//然后调用RootviewController中的方法
self.window.rootViewController = rootViewController;
//以前用这种方式
// [self.window addSubview:rootViewController.view];
NSLog(@"创建视图完成");
2.视图控制器根视图的加载
视图控制器加载的时候先去判断是否有view
(1)没有view
调用loadView方法
1)是否重写了
①重写了
是否为空,不为空的话就可以直接用,如果为空在loadView加载完继续加载viewDIdLoad使用self.view的时候会重新调用loadview,那么就会陷入递归死循环。
②没有重写
看是否有storyBoard或者xib,如果有就用storyBoard或者xib,然后加载viewDidLoad
如果没有系统会给一个empty view ,然后加载viewDidLoad
(2)
如果已经创建了view了,就直接return view
所以load或者viewDidLoad每次进来只会执行一次
//如果写view的初始化方法的话
//当控制器在初始化的时候就执行这个方法 在self.view的时候开始找view
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.view.backgroundColor = [UIColor brownColor];
}
return self;
}
//复写loadView方法
//调用load方法一定要给自己一个view
/*
调用loadView满足的条件
1、view属性的get方法被调用时[self.view]
2、view为空时
只有当以上的两个方法同时满足时,才会被调用loawView
*/
//- (void)loadView
//{
// //当loadView里边没有内容的时候,因为被重写了,所以被调用时,会循环调用,最终崩溃
// NSLog(@"这是loadView的方法");
// //如果创建了view,能找到了View了,就不会一直循环,就会return view;
// UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// view.backgroundColor = [UIColor redColor];
// //将这个view给self.view
// self.view = view;
//
//}
/*
如果不复写loadView方法的话,将会找是否有Storyboard 或者是xib
1.如果找不到 将会自动创建一个empty view
2.如果能找到,就加载那上边的view (另一个工程)
*/
//视图开始加载时 在此之前先执行loadView
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"这是viewDidLoad方法");
self.view.backgroundColor = [UIColor grayColor];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 30)];
lable.backgroundColor = [UIColor blueColor];
[self.view addSubview:lable];
//当控制器在初始化的时候就执行这个方法 在self.view的时候开始找view
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.view.backgroundColor = [UIColor brownColor];
}
return self;
}
//复写loadView方法
//调用load方法一定要给自己一个view
/*
调用loadView满足的条件
1、view属性的get方法被调用时[self.view]
2、view为空时
只有当以上的两个方法同时满足时,才会被调用loawView
*/
//- (void)loadView
//{
// //当loadView里边没有内容的时候,因为被重写了,所以被调用时,会循环调用,最终崩溃
// NSLog(@"这是loadView的方法");
// //如果创建了view,能找到了View了,就不会一直循环,就会return view;
// UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// view.backgroundColor = [UIColor redColor];
// //将这个view给self.view
// self.view = view;
//
//}
/*
如果不复写loadView方法的话,将会找是否有Storyboard 或者是xib
1.如果找不到 将会自动创建一个empty view
2.如果能找到,就加载那上边的view (另一个工程)
*/
//视图开始加载时 在此之前先执行loadView
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"这是viewDidLoad方法");
self.view.backgroundColor = [UIColor grayColor];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 30)];
lable.backgroundColor = [UIColor blueColor];
[self.view addSubview:lable];
}
//此文件与同名的.xib文件绑定在一起的,如果将两者之间的连线断开的话,就无法加载
//如果是重新加的文件,不同名,那么需要先链接,然后更改以下init方式
//initWithNibName不要加.xib
RootViewController *rootVC = [[RootViewController alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];
//加载Storyboard 或者xib中的文件
NSLog(@"开始加载");
//如果是重新加的文件,不同名,那么需要先链接,然后更改以下init方式
//initWithNibName不要加.xib
RootViewController *rootVC = [[RootViewController alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];
//加载Storyboard 或者xib中的文件
NSLog(@"开始加载");
self.window.rootViewController = rootVC;
//初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(@"这是初始化");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// self.view.backgroundColor = [UIColor grayColor];
}
return self;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(@"这是初始化");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// self.view.backgroundColor = [UIColor grayColor];
}
return self;
}
3.模态视图
//创建弹出来的控制器对象
DeailViewController *deailCon = [[DeailViewController alloc] init];
//创建弹出的动画效果
/*
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
*/
deailCon.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//弹出模态视图
//ios6.0之前
// [self presentModalViewController:deailCon animated:YES];
//ios6.0之后
//presentViewController 哪个视图控制器
//animated 是否要动画
//completion 执行完之后要做的事
[self presentViewController:deailCon animated:YES completion:^{
NSLog(@"已经弹出了模态视图");
}];
//获取当前版本
NSString *version = [UIDevice currentDevice].systemVersion;
DeailViewController *deailCon = [[DeailViewController alloc] init];
//创建弹出的动画效果
/*
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
*/
deailCon.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//弹出模态视图
//ios6.0之前
// [self presentModalViewController:deailCon animated:YES];
//ios6.0之后
//presentViewController 哪个视图控制器
//animated 是否要动画
//completion 执行完之后要做的事
[self presentViewController:deailCon animated:YES completion:^{
NSLog(@"已经弹出了模态视图");
}];
//获取当前版本
NSString *version = [UIDevice currentDevice].systemVersion;
NSLog(@"%@", version);
//关闭模态视图
//ios 6.0 之前
// [self dismissModalViewControllerAnimated:YES];
//ios 6.0之后
//ios 6.0 之前
// [self dismissModalViewControllerAnimated:YES];
//ios 6.0之后
[self
dismissViewControllerAnimated:YES
completion:nil];
//视图将要出现在屏幕之前
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"视图将要出现在屏幕之前");
}
//视图在屏幕上渲染完成
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"视图在屏幕上渲染完成");
}
//视图将被从屏幕上移除
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"视图将被从屏幕上移除");
}
//视图已经从屏幕上移除
- (void)viewDidDisappear:(BOOL)animated
{
NSLog(@"视图已经从屏幕上移除");
}
//根视图生命周期并没有销毁,但是其他的视图控制器在移除后会销毁
- (void)dealloc
{
NSLog(@"root dead");
}
4.屏幕旋转的方向
//设置控制器支持的旋转方向
- (NSUInteger)supportedInterfaceOrientations
{
//支持所有的方向
return UIInterfaceOrientationMaskAll;
//支持默认的方向是除了向下的
// return UIInterfaceOrientationMaskAllButUpsideDown;
{
//支持所有的方向
return UIInterfaceOrientationMaskAll;
//支持默认的方向是除了向下的
// return UIInterfaceOrientationMaskAllButUpsideDown;
}
//当控制器旋转时调用的方法
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
switch (toInterfaceOrientation) {
case UIDeviceOrientationLandscapeRight:
case UIInterfaceOrientationLandscapeRight:
{
//左右翻转的时候,坐标跟原来屏幕的关系
UIView *view = [self.view viewWithTag:10];
view.frame = CGRectMake((height - 100)/2, (weight - 100)/2, 100, 100);
break;
}
case UIInterfaceOrientationPortraitUpsideDown:
case UIInterfaceOrientationPortrait:
{
//上下翻转的时候
UIView *view = [self.view viewWithTag:10];
view.frame = CGRectMake((weight - 100)/2, (height - 100)/2, 100, 100);
break;
}
default:
break;
}
{
switch (toInterfaceOrientation) {
case UIDeviceOrientationLandscapeRight:
case UIInterfaceOrientationLandscapeRight:
{
//左右翻转的时候,坐标跟原来屏幕的关系
UIView *view = [self.view viewWithTag:10];
view.frame = CGRectMake((height - 100)/2, (weight - 100)/2, 100, 100);
break;
}
case UIInterfaceOrientationPortraitUpsideDown:
case UIInterfaceOrientationPortrait:
{
//上下翻转的时候
UIView *view = [self.view viewWithTag:10];
view.frame = CGRectMake((weight - 100)/2, (height - 100)/2, 100, 100);
break;
}
default:
break;
}