iOS Development-44: Merging, formatting output, macro variables, using arrays to store data dictionaries, and using plist final knowledge

Source: Internet
Author: User

Our case today is the first example of the following 5一来 by toggling the previous page on the next page:



(1) The first step, basically is in a very silly very direct way to create. The main points to be used here are:

--Set the object variable to a global variable so that it can be called in other ways to set their properties

--Set a global variable index, which is 0 by default. Then, by adding a lower index value and combining switch to invoke different data.

--Initialize the page by invoking the change method first. Causes the page to freeze in the first frame.

--Use the button's Enabled property to set whether the button can be clicked, and then combine the value of index in the 1th Zhang and 5th Zhang Shi respectively to set the previous and Next button to gray and not clickable. Of course, when initializing the page, you should also infer that the previous button is grayed out.

--The Change method and the Btncheck method here are the product of code refactoring, because both methods need to be called in the Preone and Nextone methods, so in order to avoid repeated code, so the repeated parts are encapsulated into a method.

#import "ViewController.h"//To define some objects as global variables.    This enables multiple methods to call @interface Viewcontroller () {UIButton *btnpre;    UIButton *btnnext;    UILabel *numlabel;    UILabel *desclabel;    Uiimageview *imgview1; int index1;}    @end @implementation viewcontroller-(void) viewdidload {Btnpre=[uibutton buttonwithtype:uibuttontyperoundedrect];    Btnpre.frame=cgrectmake (50, 150, 60, 30);    [Btnpre settitle:@ "on a" forstate:uicontrolstatenormal];    [Btnpre addtarget:self Action: @selector (PreOne) forcontrolevents:uicontroleventtouchupinside];    [Self.view Addsubview:btnpre];    Btnnext=[uibutton Buttonwithtype:uibuttontyperoundedrect];    Btnnext.frame=cgrectmake (250, 150, 60, 30);    [Btnnext settitle:@ "Next" forstate:uicontrolstatenormal];    [Btnnext addtarget:self Action: @selector (Nextone) forcontrolevents:uicontroleventtouchupinside];        [Self.view Addsubview:btnnext];    Numlabel=[[uilabel Alloc]init];    Numlabel.frame=cgrectmake (170, 50, 100, 30);      [Self.view Addsubview:numlabel];  Desclabel=[[uilabel Alloc]init];    Desclabel.frame=cgrectmake (110, 300, 200, 30);        [Self.view Addsubview:desclabel];    Imgview1=[[uiimageview Alloc]init];    Imgview1.frame=cgrectmake (130, 100, 100, 100);        [Self.view Addsubview:imgview1]; Since the index at the beginning is 0.        So we call the change method directly, equivalent to the first frame of the page paged out to initialize the page [self change];        Btnpre.enabled= (index1!=0);    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.}    -(void) preone{index1--;    [Self btncheck]; [Self change];}    -(void) nextone{index1++;    [Self btncheck]; [Self change];} The Enabled property of the button, assuming no, turns gray.

In fact, the following statement is a three-mesh operation pushed to and-(void) btncheck{btnpre.enabled= (index1!=0); Btnnext.enabled= (index1!=4);} -(void) change{switch (index1) {case 0: [email protected] "1/5"; [Email protected] "This was the letter A"; Imgview1.image=[uiimage imagenamed:@ "A.png"]; Break Case 1: [email protected] "2/5"; [Email protected] "This was the letter B"; Imgview1.image=[uiimage imagenamed:@ "B.png"]; Break Case 2: [email protected] "3/5"; [Email protected] "This was the letter C"; Imgview1.image=[uiimage imagenamed:@ "C.png"]; Break Case 3: [email protected] "4/5"; [Email protected] "This was the letter D"; Imgview1.image=[uiimage imagenamed:@ "D.png"]; Break Case 4: [email protected] "5/5"; [Email protected] "This was the letter E"; Imgview1.image=[uiimage imagenamed:@ "E.png"]; Break Default:break; }} @end


(2) Transform the switch: Use the formatted output to refactor the code: replace the 5 lines of code that you stare out with the second line of code.

-(void) change{numlabel.text=[nsstring stringwithformat:@ "%d/%d", index1+1,5];            Switch (index1) {case 0://[email protected] "1/5";            [Email protected] "This was the letter A";            Imgview1.image=[uiimage imagenamed:@ "A.png"];        Break            Case 1://[email protected] "2/5";            [Email protected] "This was the letter B";            Imgview1.image=[uiimage imagenamed:@ "B.png"];        Break            Case 2://[email protected] "3/5";            [Email protected] "This was the letter C";            Imgview1.image=[uiimage imagenamed:@ "C.png"];        Break            Case 3://[email protected] "4/5";            [Email protected] "This was the letter D";            Imgview1.image=[uiimage imagenamed:@ "D.png"];        Break            Case 4://[email protected] "5/5";            [Email protected] "This was the letter E"; Imgview1.image=[uIImage imagenamed:@ "E.png"];        Break    Default:break; }}

(3) The use of dictionaries and arrays to separate the data, and to achieve the deletion of data and the independence between the code, that is, the addition or subtraction of data, we do not need to change the "total number of pages" and other such code, but increase or decrease the data still need to be achieved by adding or subtracting the (Define a global variable at the beginning Nsarray *arr1)

-(void) Viewdidload {...    Nsmutabledictionary *dic1=[nsmutabledictionary Dictionary];    dic1[@ "icon"][email protected] "a.png";        dic1[@ "desc"][email protected] "This was the letter A";    Nsmutabledictionary *dic2=[nsmutabledictionary Dictionary];    dic2[@ "icon"][email protected] "b.png";        dic2[@ "desc"][email protected] "This was the letter B";    Nsmutabledictionary *dic3=[nsmutabledictionary Dictionary];    dic3[@ "icon"][email protected] "c.png";        dic3[@ "desc"][email protected] "This was the letter C";    Nsmutabledictionary *dic4=[nsmutabledictionary Dictionary];    dic4[@ "icon"][email protected] "d.png";        dic4[@ "desc"][email protected] "This is the letter D";    Nsmutabledictionary *dic5=[nsmutabledictionary Dictionary];    dic5[@ "icon"][email protected] "e.png";        dic5[@ "desc"][email protected] "This was the letter E";    Arr1=[nsarray arraywithobjects:dic1,dic2,dic3,dic4,dic5, nil]; The above code needs to be added to the。        Otherwise this initialization is a [self change] with no data to initialize;        Btnpre.enabled= (index1!=0);    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.}

After initialization of the data. The rest of the place can be simplified. For example, you can use dictionaries and arrays to fetch data by calculating the number of pages, for example, by taking data:

-(void) btncheck{    btnpre.enabled= (index1!=0);    Btnnext.enabled= (index1!=arr1.count-1);//Count the number of pages related code}

-(void) change{    numlabel.text=[nsstring stringwithformat:@ "%d/%d", index1+1,5];    Remove the corresponding data    nsdictionary *DIC=ARR1[INDEX1];    Set icon image    imgview1.image=[uiimage imagenamed:dic[@ "icon"];    Set descriptive narrative text    desclabel.text=dic[@ "desc";}

The advantage of this step is that we only need to increase or decrease the new dic6 and so on when we increase or decrease the data. Then add the dic6 and so on to the array arr1 can.

The rest of the place does not have to manually change the numbers, because we use the Count property of the array where we refer to the numbers. Where the data is referenced, the related properties of the array and the dictionary are used, not written dead, but alive.


(4) Use macro variables to avoid the possibility of errors in code, especially when collaborating with multiple developers. Use the hint of a macro variable to reduce the possibility of false input.

#define Kiconkey @ "icon" #define KDESCRIP @ "desc"

So all other responses should replace the macro variable.


(5) Use property to create variables, note. Although the teacher says the recommended control object is weak. And the general object with strong, but found. Objects cannot be instantiated using weak at all, so this is a temporary or strong. And so on to find out why.

The following variables of the response can be substituted with _**** or replaced with self.***.

It is recommended to use the latter here.

@interface Viewcontroller () {//    UIButton *btnpre;//    UIButton *btnnext;//    UILabel *numlabel;//    UILabel *desclabel;//    uiimageview *imgview1;//    int index1;} @property (Nonatomic,retain) UIButton *btnpre; @property (nonatomic,retain) UIButton *btnnext; @property (Nonatomic, Retain) UILabel *numlabel, @property (nonatomic,retain) UILabel *desclabel; @property (nonatomic,strong) Uiimageview * ImgView1; @property (Nonatomic,strong) Nsarray *arr1; @property (nonatomic,assign) int index1; @end

(6) Delay loading, lazy loading. The load data is initialized only when it is needed.

That is, we can put the initialization of the data type property in the getter method of this data. And make an inference whether to load again.

Our default Arr1 getter method is:

-(Nsarray *) arr1{    return _arr1;}

Change to such as the following. That is, assuming that the array is loaded with data, it is not loaded repeatedly, and is loaded with the SELF.ARR1 call to it, which is deferred loading:

-(Nsarray *) arr1{if (_arr1==nil) {///here with _ARR1 instead of SELF.ARR1 is to avoid a dead loop. Because SELF.ARR1 also calls this function.        will always call itself Nsmutabledictionary *dic1=[nsmutabledictionary dictionary];        Dic1[kiconkey][email protected] "a.png";                Dic1[kdescrip][email protected] "This was the letter A";        Nsmutabledictionary *dic2=[nsmutabledictionary Dictionary];        Dic2[kiconkey][email protected] "b.png";                Dic2[kdescrip][email protected] "This was the letter B";        Nsmutabledictionary *dic3=[nsmutabledictionary Dictionary];        Dic3[kiconkey][email protected] "c.png";                Dic3[kdescrip][email protected] "the letter C";        Nsmutabledictionary *dic4=[nsmutabledictionary Dictionary];        Dic4[kiconkey][email protected] "d.png";                Dic4[kdescrip][email protected] "This was the letter D";        Nsmutabledictionary *dic5=[nsmutabledictionary Dictionary];        Dic5[kiconkey][email protected] "e.png"; DIc5[kdescrip][email protected] "This is the letter E";    _arr1=[nsarray arraywithobjects:dic1,dic2,dic3,dic4,dic5, nil]; } return _ARR1;}

(7) Further: The data is stored independently in the plist, the subsequent increase or decrease in data is only to change the plist file, without the need to add or subtract data in the code.

Create the plist file first:



Then, reference in the code. The data that has been stared at has been stored in the plist file. Using the following lines to use the plist file can be, in the future to increase or decrease the data, just to modify the plist file, do not need to modify the code:

-(Nsarray *) arr1{if (_arr1==nil) {//nsmutabledictionary *dic1=[nsmutabledictionary dictionary];//dic1[        Kiconkey][email protected] "a.png";//dic1[kdescrip][email protected] "the letter A";//        Nsmutabledictionary *dic2=[nsmutabledictionary dictionary];//dic2[kiconkey][email protected] "b.png";// Dic2[kdescrip][email protected] "the letter B";///Nsmutabledictionary *dic3=[nsmutable Dictionary dictionary];//dic3[kiconkey][email protected] "c.png";//dic3[kdescrip][email protected ] "the letter C";///Nsmutabledictionary *dic4=[nsmutabledictionary dictionary];//Dic4[kico        Nkey][email protected] "d.png";//dic4[kdescrip][email protected] "the letter D";///        Nsmutabledictionary *dic5=[nsmutabledictionary dictionary];//dic5[kiconkey][email protected] "e.png";// dic5[kdescrip][email&Nbsp;protected] "This is the letter E";                _arr1=[nsarray arraywithobjects:dic1,dic2,dic3,dic4,dic5, nil]; Get Mainbundle.        The program Master directory nsbundle *path=[nsbundle Mainbundle];        Use the obtained mainbundle to find the file, return to the path nsstring *pathfile=[path pathforresource:@ "Imgdata" oftype:@ "plist"];    _arr1=[nsarray Arraywithcontentsoffile:pathfile]; } return _ARR1;}

(8) Supplement: How to view this Mainbundle resource library?

nsbundle* Path=[nsbundle Mainbundle] is the path to this repository. The NSBundle object is returned, which is actually a path through which you can access all the resources in the repository. In fact, where does it put it in detail? Do not need to search the Internet, directly with NSLog (@ "%@", path); Print out this path is not OK.


Follow the path above to find this Hello.app package >>> right click to display the contents of the package.

Done.


(9) The size of the picture is different, how is it good?

Generally we will set a fixed width of the Uiimageview, but the picture assumes that there are large and small how to do? This property is required to invoke the "content mode" Contentmode. That is, adjust the contents of uiimageview inside how to zoom.

The general default is to stretch the picture until it fills the entire uiviewview. This generally changes the aspect ratio of the picture, making the picture distorted. We usually use, in the case of maintaining the picture aspect ratio, as far as possible to fill the uiimageview can, this property and value is (why the above code is not joined, because we do the time to set the picture is 100*100,uiimageview is also 100* 100, so you don't need to use this property):

Self.imgview1.contentmode=uiviewcontentmodescaleaspectfit;

(10) Too much text. How to set your own initiative to change the line?

Take the desclabel of descriptive words as an example. In our case, the text is not very much, and it gives this desclabel width of 200, enough to use. All just one line. Assuming we set the width to 100, we find that the display does not fit. Finally there is a ... Represents omitting content. Set multiple lines with a Numberofline property. You can set the detailed number of line numbers such as 2,3,4. can also be used directly 0, indicating that it does not matter how many rows.

When you need to pay attention to, set multiple lines of time, your desclabel height should be enough, otherwise it is still showing ... Ellipsis. We were 30 in height and changed to 60.

    Self.desclabel.frame=cgrectmake (a);    self.desclabel.numberoflines=0;


Summary: Do it yourself. Although it is clear that the principle, but really operate, will encounter some very small but very important issues, one of the solutions, this accumulation, expected novice and difference veteran.


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

iOS Development-44: Merging, formatting output, macro variables, using arrays to store data dictionaries, and using plist final knowledge

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.