重写UIviewcontroller的初始化方法时要小心

在写项目的时候重写了VC的init方法,接着就出现了诡异的BUG~

-(instancetype)initWithDataArray:(NSArray*)array type:(NSString*)type{

if (self = [super init]) {

self.type = type;
self.view.backgroundColor = [UIColor whiteColor];
self.array = array;
self.currentindex = 0;

}

return self;

};
- (void)viewDidLoad {
   [self requestDomesticListnew];
}
- (void)requestDomesticListnew {
    [self.travelProductViewModel requestProductCategory:self.type
                                                   type:self.array[self.currentindex]
                                               PageSize:@"10"
                                              PageCount:[NSString stringWithFormat:@"%ld", (long) self.page]
                                                success:^(id data) {
 
                                                }
                                                failure:^(NSString *errMsg) {
                                                }];
}

requestDomesticListnewself.currentindex 却意外的始终为nil,打了断点以后发现代码走到self.view.backgroundColor = [UIColor whiteColor]; 这一行以后直接调用了videDialoda 所以接着调用了网络请求,导致self.currentindex还没有来得及初始化~~~网上查资料后发现,init的时候self.view还没有初始化,调用self.view会导致viewDidLoad提前加载.所以谨慎在init中调用VC的相关属性.

你可能感兴趣的:(重写UIviewcontroller的初始化方法时要小心)