iOS之UIPickerView使用(一)
来源:互联网 发布:mac flash player 编辑:程序博客网 时间:2024/04/25 20:50
#import "ViewController.h"
@interface ViewController () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak,nonatomic)IBOutletUIPickerView *pickerView;
// 水果
@property(nonatomic,weak)IBOutletUILabel *fruitLbl;
// 主菜
@property(nonatomic,weak)IBOutletUILabel *mainFoodLbl;
// 酒水
@property(nonatomic,weak)IBOutletUILabel *drinkLbl;
@property (nonatomic,strong)NSArray *foods;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 随机色
// [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0];
// [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
// [self pickerView:self.pickerView didSelectRow:0 inComponent:1];
// [self pickerView:self.pickerView didSelectRow:0 inComponent:2];
//让label启动就显示
for (int i =0; i <self.foods.count; i++) {
[selfpickerView:self.pickerViewdidSelectRow:0inComponent:i];
}
}
#pragma mark - 点击随机点餐按钮的时候调用
- (IBAction)randomBtnClick:(UIButton *)sender {
for (int i =0; i <self.foods.count; i++) {
// 1.让pickerView随机选中
// 生成随机数
// 第i组对应的范围
// 10 11 0~10
NSInteger count = [self.foods[i]count];
u_int32_t randomNumber =arc4random_uniform((int)count);
// 1.2每次的数据都不一样
// 获取当前列选中的行号
NSInteger selRow = [self.pickerViewselectedRowInComponent:i];
// 如果行号相等,重新生成
while ((int)selRow == randomNumber) {
randomNumber = arc4random_uniform((int)count);
}
[self.pickerViewselectRow:randomNumberinComponent:i animated:YES];
// 2.让label改变
[selfpickerView:self.pickerViewdidSelectRow:randomNumberinComponent:i];
}
}
#pragma mark - 代理方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// 1.获取选中的数据
NSString *selFood =self.foods[component][row];
// 2.设置给label
if (component ==0) {
self.fruitLbl.text = selFood;
} elseif (component ==1) {
self.mainFoodLbl.text = selFood;
} else {
self.drinkLbl.text = selFood;
}
}
// 返回每一行显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// 获取每一组的数据
NSArray *group =self.foods[component];
// 返回每一行显示的内容
return group[row];
}
#pragma mark - 数据源方法
// 返回有多少列"组"
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
returnself.foods.count;
}
// 返回每一组有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// 获取每一组的所有数据的数组
NSArray *group =self.foods[component];
// 返回行数
return group.count;
}
#pragma mark - 懒加载
- (NSArray *)foods {
if (_foods ==nil) {
// 1.找到文件路径
NSString *filePath = [[NSBundlemainBundle]pathForResource:@"01foods.plist"ofType:nil];
// 2.转为数组
_foods = [NSArrayarrayWithContentsOfFile:filePath];
}
return_foods;
}
@end
- iOS之UIPickerView使用(一)
- IOS之UIPickerView的使用简单介绍
- iOS之UIPickerView的使用--单列选择器
- iOS编程之UIPickerView
- IOS UIPickerView使用
- iOS 三级UIPickerView使用
- iOS UIPickerView的使用
- iOS UIPickerView 简单使用
- UIPickerView的使用(一)
- iOS学习之UIPickerView控件的简单使用
- iOS学习之UIPickerView控件的简单使用
- iOS开发入门之UIPickerView控件的简单使用
- iOS学习之UIPickerView控件的简单使用
- iOS学习之UIPickerView控件的简单使用
- iOS学习之UIPickerView控件的简单使用
- iOS学习之UIPickerView控件的简单使用
- iOS学习之UIPickerView控件的简单使用
- iOS之UIPickerView的使用--多列选择器
- POJ
- LeetCode:Longest Palindromic Substring
- 【剑指Offer】重建二叉树 解题报告
- VR系列——Oculus Rift 介绍指南:二、建议的规格
- CentOS 7上安装apache
- iOS之UIPickerView使用(一)
- 程序员面试金典——寻找下一个结点
- 安全
- [IOS APP] 夜听刘筱
- 河南省第六届ACM程序设计大赛(共7道,目前只做了三道。待续)
- shell 编程——for in 循环
- qt连接mysql数据库以及txt文本的复制操作
- java <<(左移位) 与 >>(右移位)
- CF