ios学习2-图片切换

用到的知识点有:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值