视图控制器及生命周期
import "AppDelegate.h"
import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];// 创建视图控制器对象
RootViewController rootVC = [[RootViewController alloc]init];
// 将创建好的视图控制器设置为window的根视图控制器
[self.window setRootViewController:rootVC];
/*- 模拟window类中,setRootViewController
-(viod) setRootViewController:(UIViewContorller) rootVC{
if(_rootViewController != rootVC ){
[_rootViewController release];
_rootViewController = [rootVC retain];
}
[self addSubView:_rootViewController.view];
}
*/
return YES;
}
- 模拟window类中,setRootViewController
import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
// 创建视图控制器的时候,系统会帮我们创建一个和屏幕一样宽高的视图(view)作为视图控制器的属性
// 加载视图控制器自带的视图 当使用到该视图控制器自带的view时(调用view属性的get方法的时候),才会调用loadview方法创建该view
// 视图加载
-(void)loadView{
NSLog(@"%s",func);
// [super loadView];// 如果我们使用自定义的视图,就不要调用[super loadview]
// 自定义视图 (自定义的视图frame的尺寸创建时已经给定根屏幕一样的宽高,所以无法修改)
UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
customView.backgroundColor = [UIColor redColor];
// 将自定义的视图赋给当前视图控制器的view
self.view = customView;
}
// 创建label的方法
-(UILabel *)creatLabel{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = @"title";
label.tag = 1000;
return label;
}
// 视图加载完毕
-
(void)viewDidLoad {
NSLog(@"%s",func);
[super viewDidLoad];
// self.view.backgroundColor = [UIColor yellowColor];// Do any additional setup after loading the view.
}
// 视图即将出现
-(void)viewWillAppear:(BOOL)animated{
NSLog(@"%s",func);
[super viewWillAppear:animated];
}
// 视图已经出现
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"%s",func);
[super viewDidAppear:animated];
}
// 视图即将消失
-(void)viewWillDisappear:(BOOL)animated{
NSLog(@"%s",func);
[super viewWillDisappear:animated];
}
// 视图已经消失
-(void)viewDidDisappear:(BOOL)animated{
NSLog(@"%s",func);
[super viewDidDisappear:animated];
}
// 视图被释放掉
-(void)dealloc{
// ARC下dealloc方法可以被重写,但是不能在内部使用[super dealloc]
// [super dealloc];
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}