06-image browser, 06-Browser
Introduction to the image browser Case Study
- Next we will use UIImageView, UILabel, and UIButton to implement a comprehensive case study.
Function Analysis
Master
- Change the image of UIImageView
- Measure the test taker's understanding about the contentMode of UIImageView.
- Change the text of UILabel and set the automatic line break and text center of UILabel in storyboard.
- Simple use of NSArray and NSDictionary
- Create and read Plist files
- Attribute lazy loading
- Use of viewDidLoad Method
- Multiple statuses of UIButton
Job
- Use code-only method to rewrite the small case of the image browser (do not drag the control)
Basic UILabel settings
- To make UILabel wrap automatically, set Lines to 0.
Center UILabel text
UIButton status
- Disabled (invalid or unavailable)
- If the enabled attribute is NO, it is in the disable state, indicating that the button cannot be clicked.
- Corresponding enumerated constant: UIControlStateDisabled
UIButton and UIImageView
- Similarities: images can be displayed
- Differences
Use of NSArray and NSDictionary
- When the image content is very large, the code "set content based on index" does not have scalability and needs to be changed frequently.
- To change the status quo, you can consider saving the image data line to an array. There are many dictionaries in order in the array. One dictionary represents an image data, including the image name and description.
@ Property (strong, nonatomic) NSArray * images;
- Because only image data needs to be initialized once, it is initialized in the get method.
- The method for initializing attributes in the get method is called "lazy loading" \ "delayed loading"
What is a Plist file?
- Writing data directly in the code is not a reasonable practice. If the data is frequently changed, the corresponding code should be frequently opened for modification, resulting in low code scalability.
- Therefore, you can store frequently changed data in a file and read the latest data from the file after the program starts. To change the data, directly modify the data file without modifying the code.
- Generally, you can use an attribute list file to store data such as NSArray or NSDictionary. The extension of this attribute list file is plist, which becomes a "Plist file"
Create a Plist File
Parse Plist files
- Next, parse the data in the Plist file through code.
NSBundle * bundle = [NSBundle mainBundle]; NSString * path = [bundle pathForResource: @ "imageData" ofType: @ "plist"];
- Load 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;
}
Plist file parsing process