標籤:
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @property (retain, nonatomic) NSArray *pic; 6 @property (assign, nonatomic) NSInteger index; 7 @property (weak, nonatomic) IBOutlet UILabel *lblIndex; 8 @property (weak, nonatomic) IBOutlet UILabel *lblTitle; 9 @property (weak, nonatomic) IBOutlet UIImageView *picture;10 - (IBAction)nextPicture:(id)sender;11 @property (weak, nonatomic) IBOutlet UIButton *btnnext;12 13 - (IBAction)lastPicture:(id)sender;14 @property (weak, nonatomic) IBOutlet UIButton *btnLast;15 16 @end17 @implementation ViewController18 19 20 - (void)viewDidLoad {21 [super viewDidLoad];22 //讓第一個圖片顯示出來,所以先調用一次23 [self nextPicture:nil];24 }25 26 - (void)didReceiveMemoryWarning {27 [super didReceiveMemoryWarning];28 }29 30 // 重寫pic屬性get方法---懶載入資料31 - (NSArray *)pic32 { /*33 寫代碼載入picture.plist檔案中的資料到_pic;34 1.擷取picture.plist檔案的路徑35 ps:[NSBundle mainBundle]擷取這個app安裝到手機上時的根目錄36 然後在根目錄下搜尋picture.plist檔案路徑37 */38 if (_pic == nil) {39 NSString *string = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@".plist"];40 NSArray *array = [NSArray arrayWithContentsOfFile:string];41 //NSLog(@"%@",array);42 43 _pic = array;44 45 }46 return _pic;47 }48 49 - (IBAction)nextPicture:(id)sender {50 //1.讓索引自加51 _index++;52 //2.從數組中擷取當前這張圖片的資料53 NSDictionary *dict = self.pic[self.index-1];54 //3.把擷取到得資料設定給介面上的控制項55 self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];56 //4.通過image屬性來設定圖片框裡面的圖片57 self.picture.image =[UIImage imageNamed: dict[@"picture"]];58 59 self.lblTitle.text = dict[@"title"];60 //設定“下一張”按鈕是否可以點擊61 self.btnnext.enabled =( _index != self.pic.count);62 //設定“上一張”按鈕是否可以點擊63 self.btnLast.enabled =( _index-1 != 0);64 65 }66 67 - (IBAction)lastPicture:(id)sender {68 _index--;69 NSDictionary *dict = self.pic[self.index-1];70 self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];71 self.picture.image =[UIImage imageNamed: dict[@"picture"]];72 self.lblTitle.text = dict[@"title"];73 74 self.btnLast.enabled =( _index-1 != 0);75 self.btnnext.enabled =( _index != self.pic.count);76 }77 @end
開發思路:
1.完成準系統
2.考慮效能
(1)(初始化操作,可以直接調用change進行)
(2)因為要控制序號和圖片兩個變數,所以考慮使用字典代替掉switch
(3)每次點擊,字典都需要建立一次,效率地下,可以考慮建立的這部分拿到初始化方法中去,這樣就只需要建立一次就ok了。
(4)考慮缺點(對代碼的順序要求極其嚴格)
(5)懶載入(需要的時候才載入,那麼什麼時候是需要的時候,及調用get方法的時候)
(6)每次都來一下?效率低下—》只有第一次調用get方法時為空白,此時執行個體化並建立數組,其他時候直接返回成員變數(僅僅執行一次)
注意點:
1.方法的呼叫堆疊(順序)。
2.使用plist:讓資料的操作更加靈活,把資料弄到外面去,解除耦合性,讓耦合性不要太強。實際上是一個xml,是蘋果定義的一種特殊格式的xml。
3.bundle-包(唯讀)
iOS-UI篇—簡單的瀏覽器查看程式