ios学习2-图片切换

来源:互联网 发布:kindle软件怎么用 编辑:程序博客网 时间:2024/06/05 01:57

用到的知识点有:

1.storyboard和viewcontroller.m之间的连线

2.资源文件.plist  使用

3.NSDictionary类的使用

4.NSArray使用

5.UIImageView使用


自己跟着视屏学习做的第二个例子:一共五张图片,点击按钮左右切换显示图片,功能很基础。


写ios程序的步骤:写布局,连线,写action等

就几个布局,拖一拖就好。连线也是。代码如下

#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:@"imageData" 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 that can be recreated.}- (IBAction)right:(UIButton *)sender {    self.currentPosition++;    [self changeImage];}- (IBAction)left:(UIButton *)sender {    self.currentPosition--;    [self changeImage];}@end

初学者,可能理解的不深刻,会先执行viewDidLoad()方法。赋值给currentPosition,然后切换界面。

主要是学习了这些语句的使用:

1.重写了imagesData方法,这样用点运算,获取变量时,就会对变量进行赋值。

-(NSArray *)imagesData{    if (_imagesData == nil) {        NSString *path = [[NSBundle mainBundle] pathForResource:@"imageData" ofType:@"plist"];        _imagesData = [NSArray arrayWithContentsOfFile:path];    }        return _imagesData;}
同时,学习了plist文件的使用,plist文件本质是一个xml文件,可以当作数据进行初始化的数据,先获取路径path,然后根据path初始化数组。

2.nsbutton设置状态:起到改变颜色的效果

self.rightBtn.enabled = (self.currentPosition != (self.imagesData.count - 1));

3.NSDictionary对象调用的时候用   obj[@“属性名字”]

self.descLable.text = self.imagesData[self.currentPosition][@"desc"];

0 0
原创粉丝点击