OC学习之UI笔记二


UIView控件
字典
懒加载
1.UIView控件 用来显示图片 显示图片的方法 
UIImage *image=[UIImage ImageNamed:@”图片名”];
Self.uimage.view=image;

2.字典
字典有两种:NSdictionary(静态)和NSMUtabledictionary(动态)
NSMutabledictionary *dictionary=[NSMutabledictionary dictionary]
Dict[@”icon”]=@”值”;
Dict[@”desc”=@”值”;
3.懒加载 为了优化性能。实际上是改写get方法当发现没有数据时才开始加载数据。
4.
If(_images==nil){
数据的初始化

}
Return _images;//
//  ViewController.m
//  03-图片浏览器
//
//  Created by feiyue on 17/4/24.
//  Copyright © 2017年 HCSLiujing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
- (IBAction)preVious;
- (IBAction)next;
@property(nonatomic,assign)int index;
@property (weak, nonatomic) IBOutlet UILabel *NOLabel;
@property (weak, nonatomic) IBOutlet UIImageView *head;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIButton *btnPre;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;
//放置数据
//一般对象用strong,UI控件用weak
@property(nonatomic,strong)NSArray *images;

@end

@implementation ViewController

- (void)viewDidLoad {
    //可变字典
    
    //为了性能问题使用了懒加载 
//    NSMutableDictionary *dict1=[NSMutableDictionary dictionary];
//    dict1[@"icon"]=@"biaoqingdi";
//    dict1[@"desc"]=@"No1";
//    NSMutableDictionary *dict2=[NSMutableDictionary dictionary];
//    dict2[@"icon"]=@"wangba";
//    dict2[@"desc"]=@"No2";
//    NSMutableDictionary *dict3=[NSMutableDictionary dictionary];
//    dict3[@"icon"]=@"bingli";
//    dict3[@"desc"]=@"No3";
//    NSMutableDictionary *dict4=[NSMutableDictionary dictionary];
//    dict4[@"icon"]=@"chiniupa";
//    dict4[@"desc"]=@"No4";
//    NSMutableDictionary *dict5=[NSMutableDictionary dictionary];
//    dict5[@"icon"]=@"danteng";
//    dict5[@"desc"]=@"No5";
//    _images=@[dict1,dict2,dict3,dict4,dict5];
   /** [super viewDidLoad];
   self.NOLabel.text=@"1/5";
    self.head.image=[UIImage imageNamed:@"biaoqingdi"];
    self.descLabel.text=@"No1";
    */
    
    
    [self changData];
    
    
}

// 懒加载:重写get方法
//核心思想:当数据不存在的时候加载
-(NSArray *)images{
    if (_images==nil) {//数据不存在的时候加载
        NSMutableDictionary *dict1=[NSMutableDictionary dictionary];
        dict1[@"icon"]=@"biaoqingdi";
        dict1[@"desc"]=@"No1";
        NSMutableDictionary *dict2=[NSMutableDictionary dictionary];
        dict2[@"icon"]=@"wangba";
        dict2[@"desc"]=@"No2";
        NSMutableDictionary *dict3=[NSMutableDictionary dictionary];
        dict3[@"icon"]=@"bingli";
        dict3[@"desc"]=@"No3";
        NSMutableDictionary *dict4=[NSMutableDictionary dictionary];
        dict4[@"icon"]=@"chiniupa";
        dict4[@"desc"]=@"No4";
        NSMutableDictionary *dict5=[NSMutableDictionary dictionary];
        dict5[@"icon"]=@"danteng";
        dict5[@"desc"]=@"No5";
        _images=@[dict1,dict2,dict3,dict4,dict5];
    }
    return _images;
}


- (IBAction)preVious {
    //索引值减一
    self.index--;
   
    [self changData];
}

- (IBAction)next {
    //1,索引值加一
    //self.index=self.index+1;
    self.index++;
    //2.根据索引值设置数据
    [self changData];
}
-(void)changData{
    self.NOLabel.text=[NSString stringWithFormat:@"%d/%d",self.index+1,  self.images.count];
    
    
    NSDictionary *dict= self.images[self.index];
    self.head.image=[UIImage imageNamed:dict[@"icon"]];
    self.descLabel.text=dict[@"desc"];
    //2.根据索引值设置数据
  /**  switch (self.index) {
        case 0:
           
            self.head.image=[UIImage imageNamed:@"biaoqingdi"];
            self.descLabel.text=@"表情1";
            break;
            
        case 1:
            
            self.head.image=[UIImage imageNamed:@"wangba"];
            self.descLabel.text=@"表情2";
            break;  case 2:
            
            self.head.image=[UIImage imageNamed:@"bingli"];
            self.descLabel.text=@"表情3";
            break;  case 3:
           
            self.head.image=[UIImage imageNamed:@"chiniupa"];
            self.descLabel.text=@"表情4";
            break;  case 4:
            //self.NOLabel.text=@"5/5";
            self.head.image=[UIImage imageNamed:@"danteng"];
            self.descLabel.text=@"表情5";
            break;
    }
   */
    //设置按钮状态
    
     /**  if (self.index==0) {
        self.btnPre.enabled=NO;
        
    }else{
        self.btnPre.enabled=YES;
    }*/
    self.btnPre.enabled=(self.index==0)?NO:YES;

    
   /** if (self.index==4) {
        self.btnNext.enabled=NO;
    }else{
        
        self.btnNext.enabled=YES;
        
    }*/
    self.btnNext.enabled=(self.index==self.images.count-1)?NO:YES;

}
@end

你可能感兴趣的:(学习笔记)