iOS Learning 2-picture Toggle

Source: Internet
Author: User

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

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.