码迷,mamicode.com
首页 > 移动开发 > 详细

ios学习2-图片切换

时间:2015-05-18 09:09:53      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

用到的知识点有:

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"];

ios学习2-图片切换

标签:

原文地址:http://blog.csdn.net/u013173289/article/details/45795365

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!