IOS第11天(1:UIPickerView点餐)
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点餐)的更多相关文章
- IOS第11天(3:UIPickerView省市联动)
********* #import "ViewController.h" #import "Province.h" @interface ViewControl ...
- IOS第11天(2:UIPickerView自定义国旗选择)
国旗选择 #import "HMViewController.h" #import "HMFlag.h" #import "HMFlagView.h& ...
- iOS边练边学--UIPickerView和UIDatePicker的简单使用
一.点菜系统练习(UIPickerView) <1>UIPickerView的常用代理方法介绍 #pragma mark - <UIPickerViewDelegate> // ...
- iOS 学习 - 11.圆角(小于等于四个)类似气泡和计算字符高度
使用贝塞尔曲线, // 小于四个角 圆角 -(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @l ...
- iOS学习11之OC继承
面向对象的三大特性:封装,继承,多态. 1.继承 继承既能保证类的完整,又能简化代码. 把公共的⽅法和实例变量写在⽗类⾥,⼦类只需要写⾃⼰独有的实例变量和⽅法即可. 继承是⾯向对象三⼤特性之⼀,合理的 ...
- IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)
***控制层 #import "ViewController.h" #import "CZKeyboardToolbar.h" @interface ViewC ...
- AJ学IOS(11)UI之图片自动轮播
AJ分享,必须精品 先看效果 代码 #import "NYViewController.h" #define kImageCount 5 @interface NYViewCont ...
- iOS - UIPickerView
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITa ...
- iOS 资源大全
这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...
随机推荐
- node.js整理 05进程管理
简介 NodeJS可以感知和控制自身进程的运行环境和状态,也可以创建子进程并与其协同工作,这使得NodeJS可以把多个程序组合在一起共同完成某项工作,并在其中充当胶水和调度器的作用 常用API Pro ...
- 运行page页面时的事件执行顺序
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- BFS(染色) LA 3977 Summits
题目传送门 题意:题意坑爹.问符合条件的的山顶个数 分析:降序排序后从每个点出发,假设为山顶,如果四周的点的高度>h - d那么可以走,如果走到已经走过的点且染色信息(山顶高度)不匹配那么就不是 ...
- three.js光源
在Threejs中,光源用Light表示,它是所有光源的基类.它的构造函数是: THREE.Light ( hex ) 它有一个参数hex,接受一个16进制的颜色值.例如要定义一种红色的光源,我们可以 ...
- Android的Proxy/Delegate Application框架 (主要介绍插件化开发)
1. 插件化的原理 是 Java ClassLoader 的原理:Java ClassLoader基础 常用的其他解决方法还包括:Google Multidex,用 H5 代替部分逻辑,删无用代码,买 ...
- ACM Registration system
Registration system 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 A new e-mail service "Berlandesk&q ...
- WebBrowser 多线程 DocumentCompleted 和定时器
备忘 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- HDU 4597 Play Game
题目链接 什么都不想说,最近状态暴跌.. #include <cstdio> #include <cstring> #include <iostream> usin ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- SDCycleScrollView 滚动视图的使用(广告)
github库链接https://github.com/gsdios/SDCycleScrollView 无限循环自动图片轮播器(一步设置即可使用) // 网络加载图片的轮播器 cycleScroll ...