iOS basic control's picture browser

Source: Internet
Author: User

Knowledge Preparation:

Similarities and differences of UIbutton and Uiimageview:

Same point: "can display pictures

Different points: "UIButton by default can listen to click events, and Uiimageview by default cannot

"UIButton can display different pictures in different states.

"UIButton can display both text and pictures.

How to choose: "UIButton: Need to display pictures, click on the pictures need to do some specific actions

Uiimageview: Just need to display the picture, click on the picture, do not need to do anything

Use of Nsarray and nsdictionary:

When the image content is very long, "set content according to index" code is not extensible, to change frequently

In order to change the status quo, you can consider the picture data line is saved to an array, the array is placed in an orderly number of dictionaries, a dictionary representing a picture data, including the picture name, picture description @property (strong, nonatomic) Nsarray *images;

Lazy load \ Deferred loading

Because only the image data needs to be initialized once, it is placed in the Get/set method to initialize/Invoke

The way that attributes are initialized in the Get method, called lazy loading \ Deferred loading

What is a plist file

"It is not a reasonable practice to directly write the data directly into the code." If the data is often changed, it is often necessary to turn over the corresponding code to modify, resulting in low code extensibility

As a result, you can consider storing frequently changing data in a file and reading the latest data from a file after the program starts. If you want to change the data, modify the data file directly, without modifying the code

"You can typically use a property list file to store data such as Nsarray or nsdictionary, which has an extension of plist and is also known as a" plist file "

Functional analysis

Click on the arrow to toggle the serial number, picture, description

"If it is the first picture, the left arrow cannot be clicked

"If it is an end picture, the right arrow cannot be clicked

Step analysis

"Build UI Interface

"Monitor button click

"Toggle serial number, picture, description

Program implementation

////KVIEWCONTROLLER.M//Image Browser////Created by Kengsir on 15-1-8.//Copyright (c) 2015 ___fullusername___. All rights reserved.////#import"KViewController.h"#defineKicon @ "icon"#defineKdesc @ "desc"@interface Kviewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @property (weak, nonatomic) Iboutlet UILabel*Nolabel, @property (weak, nonatomic) Iboutlet UILabel*Desclabel, @property (weak, nonatomic) Iboutlet UIButton*leftbtn, @property (weak, nonatomic) Iboutlet UIButton*rightbtn;-(ibaction) right;-(ibaction) left;//general objects with strong, controls with weak@property (assign,nonatomic)intindex; @property (strong,nonatomic) Nsarray*array; @end @implementation Kviewcontroller- (void) viewdidload{[Super Viewdidload]; [Self changedata];}//lazy loading of data, the way properties are initialized in the Get method,-(Nsarray *) array{if(_array ==nil) {//never initialized.//Initializing DataNsmutabledictionary *image1 =[Nsmutabledictionary dictionary]; Image1[kicon]=@"Biaoqingdi"; IMAGE1[KDESC]=@"Expression Emperor"; Nsmutabledictionary*image2 =[Nsmutabledictionary dictionary]; Image2[kicon]=@"Wangba"; IMAGE2[KDESC]=@"Bastard"; Nsmutabledictionary*image3 =[Nsmutabledictionary dictionary]; Image3[kicon]=@"Wangba"; IMAGE3[KDESC]=@"Bastard"; //quickly create an array and put the dictionary into an arraySelf.array =@[image1,image2,image3]; }    return_array;}//the scalability of the program is better-(void) changedata{//1. Change the dataSelf.noLabel.text = [NSString stringWithFormat:@"%d/%d", Self.index +1, Self.array.count]; //2. Retrieve the corresponding dictionary data according to the index of the arrayNsdictionary *imagedict =Self.array[self.index]; NSLog (@"%@", imagedict); //3. Setting up a picture//nsstring *name = Imagedict[kicon];Self.iconView.image =[UIImage Imagenamed:imagedict[kicon]]; //4. Settings DescriptionSelf.descLabel.text =Imagedict[kdesc]; //Change the button's judging state//if (Self.index = = 0) {//self.leftbtn.enabled = NO;//}else {//self.leftbtn.enabled = YES;//    }//if (Self.index = = 1) {//self.rightbtn.enabled = NO;//}else {//self.rightbtn.enabled = YES;//    }self.leftbtn.enabled = (Self.index = =0)?No:yes; Self.rightbtn.enabled= (Self.index! = Self.array.count-1);}-(ibaction) right{Self.index++ ; [Self changedata];}-(ibaction) left {Self.index--;   [Self changedata]; } @end

Effect:

optimize: Use plist to store data

use code to parse the data in the plist file to get the full path of the plist file NSBundle*bundle =[NSBundle Mainbundle]; NSString*path = [Bundle Pathforresource:@"ImageData"OfType:@"plist"]; The Get method that rewrites the initialized data that was written to be used to load the plist file _images=[Nsarray Arraywithcontentsoffile:path];-(Nsarray *) images{if(_images = =Nil) {NSBundle*bundle =[NSBundle Mainbundle]; NSString*path = [Bundle Pathforresource:@"ImageData"OfType:@"plist"]; _images=[Nsarray Arraywithcontentsoffile:path]; }    return_images;}

iOS basic control's picture browser

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.