The knowledge points used are:
The connection between 1.storyboard and VIEWCONTROLLER.M
2. resource file. plist use
Use of the 3.NSDictionary class
4.NSArray use
5.UIImageView use
The second example of learning to follow the video screen: A total of five pictures, click the button to toggle the display of pictures, the function is very basic.
Steps to write iOS programs: write layouts, connect, write action, etc.
Just a few layouts, just drag and drop. So is the connection. The code is as follows
#import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UILabel *nolable; @property ( Weak, nonatomic) Iboutlet UILabel *desclable; @property (weak, nonatomic) Iboutlet Uiimageview *mainview; @property (Weak, nonatomic) Iboutlet UIButton *leftbtn; @property (weak, nonatomic) Iboutlet UIButton *rightbtn; @property (Assign, nonatomic) int currentposition; @property (weak, nonatomic) Nsarray *imagesdata; @end @implementation viewcontroller-( Nsarray *) imagesdata{if (_imagesdata = = nil) {NSString *path = [[NSBundle mainbundle] pathforresource:@ "ImageD ATA "oftype:@" plist "]; _imagesdata = [Nsarray Arraywithcontentsoffile:path]; } return _imagesdata;} -(void) viewdidload {[Super viewdidload]; self.currentposition = 0; [Self changeimage];} -(void) changeimage {self.noLable.text = [nsstring stringwithformat:@ "%d/%d", Self.currentposition + 1,self.imagesdata . Count]; Self.descLable.text = self.imagesdata[self.currentposition][@ "deSC "]; Self.mainView.image = [UIImage imagenamed:self.imagesdata[self.currentposition][@ "icon"]; self.leftBtn.enabled = (Self.currentposition! = 0); self.rightBtn.enabled = (self.currentposition! = (self.imagesdata.count-1));} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(Ibaction) Right: (UIButton *) sender {self.currentposition++; [Self changeimage];} -(Ibaction) Left: (UIButton *) sender {self.currentposition--; [Self changeimage];} @end
Beginners, who may not understand deeply, will perform the Viewdidload () method first. Assign a value to CurrentPosition, and then switch the interface.
The main lesson is the use of these statements:
1. Rewrite the Imagesdata method so that the variable is assigned when the variable is obtained using the dot operation.
-(Nsarray *) imagesdata{ if (_imagesdata = = nil) { nsstring *path = [[NSBundle mainbundle] pathforresource:@ " ImageData "oftype:@" plist "]; _imagesdata = [Nsarray Arraywithcontentsoffile:path]; } return _imagesdata;}
At the same time, learning the use of the plist file, the plist file is essentially an XML file that can be initialized as data, first obtaining the path, and then initializing the array based on path.
2.nsbutton Setting Status: Effect of changing color
self.rightBtn.enabled = (self.currentposition! = (self.imagesdata.count-1));
3.NSDictionary object calls with obj[@ "property name"]
Self.descLable.text = self.imagesdata[self.currentposition][@ "desc"];
iOS Learning 2-picture Toggle