标签:
iOS开发中 我们经常使用懒加载
1.懒加载的好处,让控件和对象在最需要加载的时候加载。这样可以节省内存空间,因为我们移动的设备资源还是比较宝贵的。所谓懒加载 就是推迟他的getter方法的执行。
比如。一个view的子控件 ,只有当这个view被显示的时候才去加载。一个tableViewCell中,给他设置了图片,他的content View里面才包含imageView的图片,只有设置了textLabel的内容,才会加载这个textLabel.
//collectionView的数据源--属性申明
@property (nonatomic, strong) NSArray *famouseDoctorArr;
//懒加载
-(NSArray *)famouseDoctorArr{
if (_famouseDoctorArr == nil) {
_famouseDoctorArr =[FDFamousDoctorsTong famousDoctorsTongArr];
}
return _famouseDoctorArr;
}
// collectionView的懒加载属性申明
@property (nonatomic, strong) UICollectionView *collectionView;
-(UICollectionView *)collectionView{
if (_collectionView == nil) {
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout];
_collectionView.backgroundColor =[UIColor orangeColor];
// 指定代理
_collectionView.dataSource = self;
_collectionView.delegate = self;
//注册cell
// [self.collectionView registerClass:[FDIllCollectionViewCell class] forCellWithReuseIdentifier:@"illCell"];
[_collectionView registerNib:[UINib nibWithNibName:@"FDIllCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"illCell"];
}
return _collectionView;
}
//1.0---我的illView中 collection View中的 cell被点击了。
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//把模型传递给下一个控制器
FDFamousDoctorsTong *famouseDoctorTong = self.famouseDoctorArr[indexPath.item];
if (indexPath.item == self.famouseDoctorArr.count - 1) {
NSLog(@"加载公益活动界面");
}else{
//跳转到疾病选择控制器 --通知主页控制器切换
if ([self.delegate respondsToSelector:@selector(illViewDidSelectItem:)]) {
[self.delegate illViewDidSelectItem:famouseDoctorTong];
}
}
}
// 在获取这个illView中控制器中实现代理方法
#pragma mark - 疾病视图cell被点击的回调代理方法 ---执行跳转方法 传递模型
-(void)illViewDidSelectItem:(FDFamousDoctorsTong *)famouseDodctorTong{
// 从storyBoard里面加载Controller
UIStoryboard *sb =[UIStoryboard storyboardWithName:@"FDIllDetailViewController" bundle:nil];
//FDIllDetailViewController *detailVc =[sb instantiateViewControllerWithIdentifier:@"detailVC"];
FDIllDetailViewController *detailVc = sb.instantiateInitialViewController;
detailVc.title = @"疾病选择详情";
detailVc.famousDoctorsTong = famouseDodctorTong;
detailVc.view.backgroundColor =[UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1.0];
[self.navigationController pushViewController:detailVc animated:YES];
}
#pragma mark - 从主页控制器点击疾病跳转的得到参数重写模型的setter方法
-(void)setFamousDoctorsTong:(FDFamousDoctorsTong *)famousDoctorsTong{
_famousDoctorsTong = famousDoctorsTong;
NSLog(@"famousDoctorsTong.title:%@",famousDoctorsTong.title);
self.illTypeLabel.text = [NSString stringWithFormat:@"疾病类型:%@",famousDoctorsTong.title];
}
好了看似没有什么问题,但是打断点 发现 self.illTypeLabel 这个控件是nil
为什么 调用setter方法的时候还没有加载这个控制器的view,所以子控件也是没有加载的
----解决办法 把赋值的代码方法放到viewDidLoad里面去
- (void)viewDidLoad {
[super viewDidLoad];
//TODO测试
self.treatedMethodView.hidden = YES;
[self.illDetailBtn setBackgroundColor:[UIColor clearColor]];
[self.complicateBtn setBackgroundColor:[UIColor clearColor]];
[self.selectTreatMethodBtn setBackgroundColor:[UIColor clearColor]];
self.illTypeLabel.text =[NSString stringWithFormat:@"疾病类型:%@",self.famousDoctorsTong.title];
}
// 最后来一个总结
@interface ViewController ()
@property (nonatomic, strong) NSArray *shopData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_shopData = [NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]];
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSArray *)shopData
{
if (!_shopData) {
_shopData = [NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]];
}
return _shopData;
}
@end
标签:
原文地址:http://blog.csdn.net/italianshen/article/details/51360262