把loginVC视图作为根视图
LoginViewController *loginVC = [[LoginViewController alloc] init];
self.window.rootViewController = loginVC;
UIViewController的生命周期所执行方法
// 1.初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
// 2.加载视图 loadView没有被重写的时候,viewDidLoad默认返回一个空view(不满足实际的开发需求)
// loadView被重写之后,我们可以根据自己的需求去初始化一个view(self.view = view),然后返回我们一个自定义的视图
- (void)loadView {
LTView *lt = [[LTView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = lt; // 不能用addsubView
// 释放
}
// 3.视图加载完毕
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
// 4.视图即将出现
- (void)viewWillAppear:(BOOL)animated {
}
// 5.视图已经出现
- (void)viewDidAppear:(BOOL)animated {
}
// 6.视图即将消失
- (void)viewWillDisappear:(BOOL)animated {
}
// 7.视图已将消失
- (void)viewDidDisappear:(BOOL)animated {
}
二:屏幕旋转
注意视图控制器会⾃自动调整view的⼤小以适应屏幕旋转,bounds被修改,触发view的layoutSubviews方法。
// 屏幕旋转,view的大小发生变化,bounds会被修改
- (void)layoutSubviews {
// 判断当前设备的方向
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
_btn.frame = CGRectMake(20, 150, 300, 30);
} else {
_btn.frame = CGRectMake(20, 150, 200, 30);
}
}
视图控制器本⾝身能检测到屏幕的旋转,如果要处理屏幕旋转,需要重写⼏个方法:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
//- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//
//}
//
//- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//
//}
// 这个方法替换上面两个过时的方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
NSLog(@"被替换的新方法");
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSLog(@"旋转结束");
}
三:处理内存警告
// 点击模拟器-->硬件-->模拟内存警告,这个方法就会被执行
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
// 如果视图已被加载,但是视图没出来
if ([self isViewLoaded] == YES && self.view.window == nil) {
// 销毁根视图
self.view = nil;
}
}