UIView的init与initWithFrame的调用顺序

- (instancetype)init;

- (instancetype)initWithFrame:(CGRect)frame;


今日发现这两个方法的调用顺序还是有关联的。调用init的时候,会先调用initWithFrame,然后在调用init;


#import "TView.h"


@implementation TView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        NSLog(@"frame == %@",NSStringFromCGRect(frame));

        NSLog(@"%s,%d",__FUNCTION__,__LINE__);

    }

    returnself;

}


- (instancetype)init{

    self = [superinit];

    if (self) {

        NSLog(@"%s,%d",__FUNCTION__,__LINE__);

    }

    returnself;

}

@end



ViewController中

- (void)initView{

    TView *tv0 = [[TViewalloc] init];

    NSLog(@"----------");

    TView *tv1 = [[TViewalloc] initWithFrame:CGRectMake(0,0, 100,100)];

    NSLog(@"----------");

    TView *tv2 = [[TViewalloc] init];

}



log顺序如下:

2016-08-18 17:34:01.575 NavBarDemo[96155:14447952] frame == {{0, 0}, {0, 0}}

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] -[TView initWithFrame:],17

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] -[TView init],25

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] ----------

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] frame == {{0, 0}, {100, 100}}

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] -[TView initWithFrame:],17

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] ----------

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] frame == {{0, 0}, {0, 0}}

2016-08-18 17:34:01.576 NavBarDemo[96155:14447952] -[TView initWithFrame:],17

2016-08-18 17:34:01.577 NavBarDemo[96155:14447952] -[TView init],25






猜你喜欢

转载自blog.csdn.net/opentogether/article/details/52243163