需求是创建一个VC,创建后赋值参数,然后VC内部方法将利用此VC初始化时被赋值的全局变量作为参数进行网络请求,初始化如下:
1- (FlightDomesticFreeViewController *)freeDomesticFlightViewController
2{
3 if (_freeDomesticFlightViewController) {
4 return _freeDomesticFlightViewController;
5 }
6 _freeDomesticFlightViewController = [[TNBoss3FlightDomesticFreeViewController alloc] init];
7 _freeDomesticFlightViewController.adultNum = self.adultNum;
8 _freeDomesticFlightViewController.childNum = self.childNum;
9 _freeDomesticFlightViewController.currentSeqNum = 1;
10 _freeDomesticFlightViewController.baseSinglePriceArray = self.baseSinglePriceArray;
11 _freeDomesticFlightViewController.basePackagePrice = self.basePackagePrice;
12 _freeDomesticFlightViewController.productInfo = self.freeFlightInfoDict[PRODUCT_INFO];
13 _freeDomesticFlightViewController.selectedFlights = self.freeFlightInfoDict[SELECT_FLIGHT];
14 _freeDomesticFlightViewController.departureCityCode = self.freeFlightInfoDict[DEPARTURE_CITY_CODE];
15 _freeDomesticFlightViewController.backCityCode = self.freeFlightInfoDict[BACK_CITY_CODE];
16 _freeDomesticFlightViewController.queryFlight = self.queryFlight;
17 _freeDomesticFlightViewController.bookCity = self.bookCity;
18 _freeDomesticFlightViewController.delegate = self;
19 _freeDomesticFlightViewController.isChangeOrder = self.isChangeOrder;
20 _freeDomesticFlightViewController.view.frame = self.freeContentView.bounds;
21 _freeDomesticFlightViewController.journeyId = self.baseInfo.journeyId;
22 [_freeContentView addSubview:_freeDomesticFlightViewController.view];
23 [self addChildViewController:_freeDomesticFlightViewController];
24 return _freeDomesticFlightViewController;
}
然后测试告诉我入参没有journeyId这个字段,后来经过反复断点调试,发现20行执行完以后便会调入VC的内部方法,进行网络请求,而此时21行journeyId还未赋值,所以导致入参此字段为空。原因就是因为20行执行后,VC的viewDidLoad会执行,也就是开始执行VC的内部方法,故这种初始化属性方式不可在设置frame方法后,也就是将21行代码放在20行之前,便可避免这种低级错误。
网友评论