问题代码:
Class B
- (CPPhotoPreviewBottomView *)bottomView {
if (!_bottomView) {
_bottomView = [[CPPhotoPreviewBottomView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100 - CP_Bottom_Margin, self.view.frame.size.width, 100 + CP_Bottom_Margin) modelArray:self.previewPicArray manager:self.manager];
}
return _bottomView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.bottomView];
// Do any additional setup after loading the view.
}
Class A
- (void)bottomViewPreviewBtnClick
{
CPPhotoPreviewViewController *previewVC = [[CPPhotoPreviewViewController alloc] init];
previewVC.previewPicArray = [NSMutableArray arrayWithArray:self.manager.selectedList];
previewVC.manager = self.manager;
previewVC.currentModelIndex = 0;
previewVC.bottomView.currentModelIndex = 0;
[self.navigationController pushViewController:previewVC animated:YES];
}
此代码,会造成懒加载执行两次,会同时创建两个内存地址完全不同的bottomView,view会加载第二个bottomView。可能会出现后续对bottomView的操作时对第一个进行操作,进而操作失效。
原因
ClassA中的按钮方法,会创建ClassB执行ClassB中的初始化方法,并在previewVC.bottomView.currentModelIndex
中执行懒加载方法,注意此时viewdidload
并未执行。在执行懒加载中bottomView
的初始化过程中传入了self.view
作为相关参数,因此在懒加载执行完前会调用viewdidload
方法,而viewdidload
中又调用self.bottomView
,此时懒加载并未执行完,没有_bottomView
返回,因此懒加载又被重新执行一遍,最终导致执行两遍。
网友评论