UIPickerView

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong)NSArray *foods;
@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *drinkLabel; - (IBAction)randomMenu;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView; @end @implementation ViewController -(NSArray *)foods{
if (!_foods) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
_foods = [NSArray arrayWithContentsOfFile:filePath];
} return _foods;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. NSLog(@"%@",self.foods); //默认选中数据,第一行数据
// [self pickerView:nil didSelectRow:0 inComponent:0];
// [self pickerView:nil didSelectRow:0 inComponent:1];
// [self pickerView:nil didSelectRow:0 inComponent:2]; NSInteger columns = self.foods.count;
for (int i = ; i < columns; i++) {
[self pickerView:nil didSelectRow: inComponent:i];
} } #pragma mark UIPickerView数据源
#pragma mark 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return self.foods.count;
} #pragma mark 每一列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ //获取每一列的数据
NSArray *items = self.foods[component]; //返回每一列的个数据
return items.count;
} #pragma mark UIPickerView代理
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ //获取每一列的数据
NSArray *items = self.foods[component]; //返回对应列的行的数据
return items[row];
} #pragma mark pickerView的选中
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"component %ld row %ld",component,row); //获取数据
NSArray *items = self.foods[component];
NSString *rowStr = items[row]; //更改数据
switch (component) {
case ://水果
self.fruitLabel.text = rowStr;
break;
case ://主食
self.mainFoodLabel.text = rowStr;
break;
case ://饮料
self.drinkLabel.text = rowStr;
break; default:
break;
} } #pragma mark 随机菜单,由天决定我们吃什么
- (IBAction)randomMenu {
NSLog(@"xx");
// 1.获取每列有多少行
NSInteger columns = self.foods.count; for (int i = ; i < columns; i ++) { NSArray *items = self.foods[i];
//对应列的行数 NSInteger rowsInColumn = items.count; // 2.在行范围产生随机数据
NSInteger randomRow =arc4random_uniform((int)rowsInColumn); //每列的随机行数不能与之前一样
// 获取旧的行数
NSInteger oldRow = [self.pickerView selectedRowInComponent:i]; while (oldRow == randomRow ) {
randomRow =arc4random_uniform((int)rowsInColumn);
} // 0.0~0.9
//arc4random_uniform(10) * 0.1 // 0.00~0.99
//arc4random_uniform(100) * 0.01 // 3.更改数据
// 3.1 更改label的数据
[self pickerView:nil didSelectRow:randomRow inComponent:i]; #warning 要改变PIckerview选中的行,要使用pickerview的一个方法
// 3.1 更改UIPickerView选中的数据
[self.pickerView selectRow:randomRow inComponent:i animated:YES];
} }
@end

IOS第11天(1:UIPickerView点餐)的更多相关文章

  1. IOS第11天(3:UIPickerView省市联动)

    ********* #import "ViewController.h" #import "Province.h" @interface ViewControl ...

  2. IOS第11天(2:UIPickerView自定义国旗选择)

    国旗选择 #import "HMViewController.h" #import "HMFlag.h" #import "HMFlagView.h& ...

  3. iOS边练边学--UIPickerView和UIDatePicker的简单使用

    一.点菜系统练习(UIPickerView) <1>UIPickerView的常用代理方法介绍 #pragma mark - <UIPickerViewDelegate> // ...

  4. iOS 学习 - 11.圆角(小于等于四个)类似气泡和计算字符高度

    使用贝塞尔曲线, // 小于四个角 圆角 -(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @l ...

  5. iOS学习11之OC继承

    面向对象的三大特性:封装,继承,多态. 1.继承 继承既能保证类的完整,又能简化代码. 把公共的⽅法和实例变量写在⽗类⾥,⼦类只需要写⾃⼰独有的实例变量和⽅法即可. 继承是⾯向对象三⼤特性之⼀,合理的 ...

  6. IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)

    ***控制层 #import "ViewController.h" #import "CZKeyboardToolbar.h" @interface ViewC ...

  7. AJ学IOS(11)UI之图片自动轮播

    AJ分享,必须精品 先看效果 代码 #import "NYViewController.h" #define kImageCount 5 @interface NYViewCont ...

  8. iOS - UIPickerView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITa ...

  9. iOS 资源大全

    这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...

随机推荐

  1. DSP using MATLAB 示例Example2.3

    n = [-10:1:10]; alpha = -0.1+0.3j; % x = exp(alpha*n); % subplot(2,1,1); set(gcf,'Color',[1,1,1]) % ...

  2. shell-bash学习01基础、打印、环境变量

    基础 终端提示符: username@hostname$; $: 一般用户 #:root用户 运行脚本 Bash执行: * bash script.sh; 直接运行: 脚本开头添加shebang起始: ...

  3. 后缀数组 POJ 3581 Sequence

    题目链接 题意:把n个数字(A1比其他数字都大)的序列分成三段,每段分别反转,问字典序最小的序列. 分析:因为A1比其他数字都大,所以反转后第一段结尾是很大的数,相当是天然的分割线,第一段可以单独考虑 ...

  4. linux 添加新硬盘的方法

    在服务器上把硬盘接好,启动linux,以root登陆. 比如我新加一块SCSI硬盘,需要将其分成三个区: #fdisk /dev/sdb 进入fdisk模式: Command (m for help) ...

  5. uva 1339

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

  6. background-attachment 定义背景图片随滚动轴的移动方式

  7. Android 定位地理坐标体系

    参考: 中国特色 火星坐标 iOS 火星坐标相关整理及解决方案汇总 百度地图坐标转换API 地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02)百度坐标系 (BD-09) 的转换算法 火星坐标 ...

  8. Topcoder SRM 626 DIV2 SumOfPower

    本题就是求所有连续子数列的和 开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列 循环遍历即可 int findSum(vector <int> array) { ; ; ...

  9. 《深入浅出Windows Phone 8.1 应用开发》基于Runtime框架全新升级版

    <深入浅出Windows Phone 8.1 应用开发>使用WP8.1 Runtime框架最新的API重写了上一本<深入浅出Windows Phone 8应用开发>大部分的的内 ...

  10. NOI模拟赛Day3

    终于A题啦鼓掌~开心~ 开考看完题后,觉得第二题很好捏(傻叉上线 搞到十一点准备弃疗了然后突然发现我会做第一题 于是瞎码了码,就去准备饭票了... 好了,停止扯淡(就我一个我妹子每天不说话好难受QAQ ...