长沙IOS培训
达内长沙IOS培训中心

18487146383

热门课程

加载 info.plist文件

  • 时间:2016-05-30
  • 发布:长沙ios培训
  • 来源:达内新闻

plist

#import "ViewController.h"

@interface ViewController ()

/** 存放所有商品的整体 */

@property (weak, nonatomic) IBOutlet UIView *shopsView;

/** HUD */

@property (weak, nonatomic) IBOutlet UILabel *hud;

// 文档注释

/** 添加按钮 */

@property (weak, nonatomic) UIButton *addBtn;

/** 删除按钮 */

@property (weak, nonatomic) UIButton *removeBtn;

/** 全部商品数据 */

@property (strong, nonatomic) NSArray *shops;

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// 添加“添加按钮”

self.addBtn = [self addButtonWithImage:@"add" highImage:@"add_highlighted" disableImage:@"add_disabled" frame:CGRectMake(30, 30, 50, 50) action:@selector(add)];

// 添加“删除按钮”

self.removeBtn = [self addButtonWithImage:@"remove" highImage:@"remove_highlighted" disableImage:@"remove_disabled" frame:CGRectMake(270, 30, 50, 50) action:@selector(remove)];

self.removeBtn.enabled = NO;

// 加载plist数据

// 一个NSBundle对象对应一个资源包(图片、音频、视频、plis等文件)

// NSBundle的作用:用来访问与之对应的资源包内部的文件,可以用来获得文件的全路径

// 项目中添加的资源都会被添加到主资源包中

// [NSBundle mainBundle]关联的就是项目的主资源包

NSBundle *bundle = [NSBundle mainBundle];

// 利用mainBundle获得plist文件在主资源包中的全路径

NSString *file = [bundle pathForResource:@"shops" ofType:@"plist"];

// NSString *file = [bundle pathForResource:@"shops.plist" ofType:nil];

// 凡是参数名为File,传递的都是文件的全路径

self.shops = [NSArray arrayWithContentsOfFile:file];

}

#pragma mark 添加按钮

- (UIButton *)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage frame:(CGRect)frame action:(SEL)action

{

// 创建按钮

UIButton *btn = [[UIButton alloc] init];

// 设置背景图片

[btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];

[btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];

[btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled];

// 设置位置和尺寸

btn.frame = frame;

// 监听按钮点击

[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

// 添加按钮

[self.view addSubview:btn];

return btn;

}

#pragma mark 添加

- (void)add

{

// self.shopsView.clipsToBounds = YES;

// 每一个商品的尺寸

CGFloat shopW = 80;

CGFloat shopH = 90;

// 一行的列数

int cols = 3;

// 每一列之间的间距

CGFloat colMargin = (self.shopsView.frame.size.width - cols * shopW) / (cols - 1);

// 每一行之间的间距

CGFloat rowMargin = 10;

// 创建一个父控件(整体:存放图片和文字)

UIView *shopView = [[UIView alloc] init];

shopView.backgroundColor = [UIColor redColor];

// 商品的索引

NSUInteger index = self.shopsView.subviews.count;

// 商品的x值

NSUInteger col = index % cols;

CGFloat shopX = col * (shopW + colMargin);

// 商品的y值

NSUInteger row = index / cols;

CGFloat shopY = row * (shopH + rowMargin);

shopView.frame = CGRectMake(shopX, shopY, shopW, shopH);

[self.shopsView addSubview:shopView];

// 获得index位置对应的商品数据

NSDictionary *shop = self.shops[index];

// 添加图片

UIImageView *iconView = [[UIImageView alloc] init];

iconView.image = [UIImage imageNamed:shop[@"icon"]];

iconView.frame = CGRectMake(0, 0, shopW, shopW);

iconView.backgroundColor = [UIColor blueColor];

[shopView addSubview:iconView];

// 添加文字

UILabel *label = [[UILabel alloc] init];

label.text = shop[@"name"];

label.frame = CGRectMake(0, shopW, shopW, shopH - shopW);

label.font = [UIFont systemFontOfSize:11];

label.textAlignment = NSTextAlignmentCenter;

[shopView addSubview:label];

// 控制按钮的可用性

[self checkState];

}

#pragma mark 删除

- (void)remove

{

[[self.shopsView.subviews lastObject] removeFromSuperview];

// 控制按钮的可用性

[self checkState];

}

#pragma mark 检查状态:按钮状态

- (void)checkState

{

// 删除按钮什么时候可以点击:商品个数 > 0

self.removeBtn.enabled = (self.shopsView.subviews.count > 0);

// 添加按钮什么时候可以点击:商品个数 < 总数

self.addBtn.enabled = (self.shopsView.subviews.count < self.shops.count);

// 显示HUD

NSString *text = nil;

if (self.removeBtn.enabled == NO) { // 删光了

text = @"已经全部删除";

} else if (self.addBtn.enabled == NO) { // 加满了

text = @"已经添加满了";

}

if (text == nil) return;

self.hud.text = text;

self.hud.alpha = 1.0;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

self.hud.alpha = 0.0;

});

}

@end

智者创造机会,强者把握机会,弱者坐等机会。长沙ios培训机构五月班正式开班,六月班已经进入预约时间,你还在等什么呢?

上一篇:对抗Apple Pay:三星支付在华将支持支付宝
下一篇:Apple Store开始走奢侈风:这是好还是坏?

2017三季度iPhone 7畅销榜新鲜出炉

iPhone X/8GPS定位问题,在iOS 11.2修复

成都高新区突围:5年建成万亿级创新创业中心

2017下半牛诺基亚净亏损1.90亿欧

选择城市和中心
贵州省

广西省

海南省

扫一扫

了解更多干货