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. Spring事务解析3-增强方法的获取

    从InfrastructureAdvisorAutoProxyCreator的层次结构中可以看到,InfrastructureAdvisorAutoProxyCreator间接实现了SmartInst ...

  2. Something about "for"

    For语句引导了一个循环语句,格式for(::),例for(int i=0;i<100;i++).类似于if()括号的作用for()括号如同if()括号一样也是一个boolean型.int i= ...

  3. Ipython console in Spyder stuck on “connecting to kernel”

    简短地记录下,今天排除的spyder的BUG, 现象:打开Spyder时其他正常,但是Ipython console 不能正常获取到kernel,一直转圈,显示“connecting to kerne ...

  4. Android App 性能优化实践

    本文记录了Android App优化需要用到的工具和以及在实践中的Tips.也算对我这半年来部分工作的总结. 工具 Hierarchy Viewer 是 Android SDK 自带的 Layout ...

  5. ArcGIS 点到直线的距离

    /****点到直线的距离*** * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0 * 设直线斜率为K = (y2-y1)/(x2 ...

  6. JS扩展方法——字符串trim()

    转自:http://www.cnblogs.com/kissdodog/p/3386480.html <head> <title>测试JS扩展方法</title> ...

  7. HDU5863 cjj's string game(DP + 矩阵快速幂)

    题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...

  8. POJ1815 Friendship(字典序最小最小割割边集)

    看了题解.当时也觉得用邻接矩阵挺好写的,直接memset:然而邻接矩阵不懂得改,于是就放开那个模板,写了Dinic.. 方法是,按字典序枚举每一条满流的边,然后令其容量减1,如果最大流改变了,这条边就 ...

  9. Handling events in an MVVM WPF application

      Posted: June 30, 2013 | Filed under: MVVM, WPF, XAML |1 Comment In a WPF application that uses the ...

  10. HDU-I Hate It

    Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...