让我们荡起双桨,小船儿推开波浪。。。。
1.模拟系统单例类,且alloc崩掉,比如UIApplication
Person:
+(void)load
{
[super load];
NSLog(@"%@-%@",NSStringFromClass(self),NSStringFromSelector(_cmd));
person=[[self alloc]init];
}
+(instancetype)sharePerson
{
return person;
}
//重写alloc,抛出异常。
+(instancetype)alloc
{
if(person)
{
NSException * exc=[NSException exceptionWithName:@"不能Init" reason:@"就是不让" userInfo:@{@"属性":@"傻"}];
[exc raise];
}
return [super alloc];
}
Person * person=[Person sharePerson];
Person * person2=[Person sharePerson];
NSLog(@"%p,%p",person,person2);//相同
Person * person3=[[Person alloc]init];//奔溃
2.load、initialize、init
//main:
int main(int argc, char * argv[]) {
NSLog(@"main");
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
//Person:
+(void)load
{
NSLog(@"%@-%@",NSStringFromClass(self),NSStringFromSelector(_cmd));
}
+(void)initialize
{
NSLog(@"%@-%@",NSStringFromClass(self),NSStringFromSelector(_cmd));
}
-(instancetype)init
{
if(self=[super init])
{
NSLog(@"%@-%@",NSStringFromClass(self.class),NSStringFromSelector(_cmd));
}
return self;
}
vc:
- (void)viewDidLoad {
[super viewDidLoad];
Person * person=[[Person alloc]init];
//Son * son=[[Son alloc]init];
打印顺序:
2018-05-18 17:33:11.555111+0800 TestLayout[3744:443588] Person-load
2018-05-18 17:33:11.649398+0800 TestLayout[3744:443588] main
2018-05-18 17:33:13.013905+0800 TestLayout[3744:443588] Person-initialize
2018-05-18 17:33:13.014013+0800 TestLayout[3744:443588] Person-init
- load是在类加载,main函数运行之前调用,也就是即便不创建该类实例也会被调用,且只调用一次。
- initialize优先于init调用,但都是在该类或者子类实例使用时才会调用,也就是不一定只调用一次。
创建Son的实例对象:
-(instancetype)init
{
if(self=[super init])
{
NSLog(@"1%@-%@",NSStringFromClass(self.class),NSStringFromSelector(_cmd));
NSLog(@"2%@-%@",NSStringFromClass(super.class),NSStringFromSelector(_cmd));
NSLog(@"3%@-%@",NSStringFromClass(self.superclass),NSStringFromSelector(_cmd));
}
return self;
}
打印结果
2018-06-04 12:32:23.064179+0800 TestLayout[3166:216280] 1Son-init
2018-06-04 12:32:23.064265+0800 TestLayout[3166:216280] 2Son-init
2018-06-04 12:32:23.064342+0800 TestLayout[3166:216280] 3Person-init
- super的作用
直接调用父类中的某个方法,也就是super在对象方法中,那么就会调用父类的对象方法
super在类方法中,那么就会调用父类的类方法
3.应用启动过程
a.执行main函数
//nil默认就是"UIApplication"
//AppDelegate其实就是UIApplication的代理UIApplicationDelegate的实现文件
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
b.执行UIApplicationMain
b1.创建UIApplication对象,并设置它的代理
b2.开启RunLoop
b3.加载Info.plist文件并做相应配置,比如Main storyboard file base name=Main,就去找Main.storyboard加载(创建UIWindow,把Main.storyboard指向的控制器作为UIWindow的rootViewController,对他保持强引用,该VC中的View由UIWindow显示出来)。
b4.AppDelegate didFinishLaunchingWithOptions 调用
- UIWindow
info.plist中配置Main storyboard file base name->空
那么,手动创建UIWindow对象
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.创建UIWindow对象
self.mainwindow=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UIViewController * vc=[[UIViewController alloc]init];
vc.view.backgroundColor=[UIColor orangeColor];
self.mainwindow.rootViewController=vc;
//2.指定UIWindow对象可见且为主窗口
[self.mainwindow makeKeyAndVisible];
return YES;
}
Main storyboard file base name->Main
那么,其实不用手动创建UIWindow对象,因为加载Main.storyboard的时候UIWindow对象被创建出来了。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController * vc=[[UIViewController alloc]init];
vc.view.backgroundColor=[UIColor orangeColor];
self.window.rootViewController=vc;
[self.window makeKeyAndVisible];
return YES;
}
假如,Main storyboard file base name->Main 且
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.创建UIWindow对象
self.mainwindow=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UIViewController * vc=[[UIViewController alloc]init];
vc.view.backgroundColor=[UIColor orangeColor];
self.mainwindow.rootViewController=vc;
//2.指定UIWindow对象可见且为主窗口
[self.mainwindow makeKeyAndVisible];
return YES;
}
那么会以哪个为主窗口呢,答案是Main,因为窗口优先级问题。
那如果要让mainwindow成为主窗口呢,就是更改mainwindow优先级更高。
self.mainwindow.windowLevel=UIWindowLevelAlert;
一个App可以拥有多个UIWindow,比如状态栏,键盘都是UIWindow。
4.UIViewController生命周期
UIViewController本身的View是懒加载的,在创建的时候会调用loadView,创建完毕调用viewDidLoad,相当于
-(UIView *)view
{
if(_view==nil)
{
[self loadView];
_view=[[UIView alloc]init];
[self viewDidLoad];
}
return _view;
}
- loadView
如果重写该方法,必须调用[super loadView],不然self.view为空。
(据说在ios9,如果重写loadView,必须手动创建self.view)
//TestViewController:
@implementation TestViewController
-(void)loadView
{
[super loadView];
self.view.backgroundColor=[UIColor redColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor orangeColor];
}
@end
//vc:
TestViewController * vc=[[TestViewController alloc]init];
vc.view.backgroundColor=[UIColor lightGrayColor];
[self presentViewController:vc animated:YES completion:nil];
结果视图显示灰色。