iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
一、实现效果
说明:点击随机按钮,能够自动选取,下方数据自动刷新。
二、实现思路


//
// YYViewController.m
// 06-简单选菜系统的实现
//
// Created by apple on 14-6-5.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYViewController.h" //遵守数据源和代理协议
@interface YYViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
/**
* 水果
*/
@property (strong, nonatomic) IBOutlet UILabel *fruitLab;
/**
* 主菜
*/
@property (strong, nonatomic) IBOutlet UILabel *stapleLab;
/**
* 饮料
*/
@property (strong, nonatomic) IBOutlet UILabel *drinkLab;
/**
* 保存所有的数据
*/
@property(nonatomic,strong)NSArray *foods;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
- (IBAction)randomFood:(id)sender; @end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad]; //在这里设置下方数据刷新部分的初始显示
for (int component = ; component<self.foods.count; component++) {
[self pickerView:nil didSelectRow: inComponent:component];
}
} #pragma mark-使用懒加载,把数据信息加载进来
-(NSArray *)foods
{
if (_foods==nil) {
NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil];
NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
_foods=arrayM;
}
return _foods;
} #pragma mark-处理随机按钮的点击事件
- (IBAction)randomFood:(id)sender { // 让pickerView主动选中某一行
// 让pickerView选中inComponent列的Row行
// [self.pickerView selectRow:1 inComponent:0 animated:YES]; /*
[self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES];
[self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES];
[self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES];
*/ // [self.foods objectAtIndex:0]; == self.foods[0];
// [self.foods[0] count]; /*
// 根据每一列的元素个数生成随机值
[self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES];
[self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES];
[self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES];
*/ //设置一个随机数
for (int component=; component<self.foods.count; component++) {
//获取当前列对应的数据元素的个数
int total=[self.foods[component] count];
//根据每一列的总数生成随机数(当前生成的随机数)
int randomNumber=arc4random()%total; //获取当前选中的行(上一次随机后移动到的行)
int oldRow=[self.pickerView selectedRowInComponent:]; //比较上一次的行号和当前生成的随机数是否相同,如果相同的话则重新生成
while (oldRow==randomNumber) {
randomNumber=arc4random()%total;
} //让pickerview滚动到指定的某一行
[self.pickerView selectRow:randomNumber inComponent:component animated:YES];
//模拟,通过代码选中某一行
[self pickerView:nil didSelectRow:randomNumber inComponent:component];
}
} #pragma mark- 设置数据
//一共多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return self.foods.count;
} //每列对应多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//1.获取当前的列
NSArray *arayM= self.foods[component];
//2.返回当前列对应的行数
return arayM.count;
} //每列每行对应显示的数据是什么
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//1.获取当前的列
NSArray *arayM= self.foods[component];
//2.获取当前列对应的行的数据
NSString *name=arayM[row];
return name;
} #pragma mark-设置下方的数据刷新
// 当选中了pickerView的某一行的时候调用
// 会将选中的列号和行号作为参数传入
// 只有通过手指选中某一行的时候才会调用
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//获取对应列,对应行的数据
NSString *name=self.foods[component][row];
//赋值
if (==component) {
self.fruitLab.text=name;
}else if(==component)
{
self.stapleLab.text=name;
}else
self.drinkLab.text=name;
} #pragma mark-隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end
四、重要补充
请注意在代码实现中为什么使用 [self.foods[0] count]; 而不是直接使用点语法self.foods[0].count取值。
[self.foods objectAtIndex:0]; == self.foods[0];//这两句的效果等价,而self调用objectAtIndex:0这个方法,返回的是一个id类型的万能指针,它的真实类型要到实际运行的时候才能检测得到,因此不能直接使用self.foods[0].count。
iOS开发UI篇—使用picker View控件完成一个简单的选餐应用的更多相关文章
- iOS开发UI篇—Quartz2D(自定义UIImageView控件)
iOS开发UI篇—Quartz2D(自定义UIImageView控件) 一.实现思路 Quartz2D最大的用途在于自定义View(自定义UI控件),当系统的View不能满足我们使用需求的时候,自定义 ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...
- iOS开发UI篇—控制器的View的创建
iOS开发UI篇—控制器的View的创建 一.6种创建控制器View的方式 #import "NJAppDelegate.h" #import "NJViewContro ...
- 【转】 iOS开发UI篇—控制器的View的创建
最近对view的周期等还不是非常清楚,就找到顶哥的文章,非常不错,就搬运过来了. 原文: http://www.cnblogs.com/wendingding/p/3770760.html 一.6种创 ...
- iOS开发UI篇—CAlayer(创建图层)
iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
随机推荐
- array_merge注意细节
array_merge:合并一个或多个数组,一个数组中的值加在前一个数组的后面,返回的新数组作为结果 如果输入的数组中有相同的字符串键名,则该键名后面的值覆盖前面的,如果数组包含相同的数字键名,后面的 ...
- .net环境下ckeditor与ckfinder中文文件链接乱码的问题
.net环境下ckeditor与ckfinder中文文件链接乱码的问题 将ckfinder.js中的getUrl:function(){return this.folder.getUrl()+enco ...
- lua 操作中文字符串之截取和长度竖排显示
前言 在游戏中,我们经常会遇到汉字的多行显示,比如名字竖行显示等.如下图: 为了实现上面的效果,lua实现分行是通过 \n 实现的,所以我们需要取出汉字,然后插入 \n 实现分行效果.还有一种就是 ...
- mac攻略(四) -- brew使用
1.介绍 brew是一个mac新用户需要了解的必备命令,它是mac下的软件包管理软件,类似centos下的yum.ubuntu下的apt-get,免去了自己手动编译安装的不方便. 很多时候作为一个用户 ...
- 调用discuz编辑器再也不是问题了
前面讲了如何开发一个discuz的特殊主题插件,详情可在此查看discuz特殊主题插件开发步骤和犯的愚蠢错误.上一篇文章讲解的是一些简单的开发步骤,不涉及到具体的编码.网页编辑器之类的都是系统默认带过 ...
- CentOS下LAMP一键yum安装脚本
本脚本适用环境: 系统支持:CentOS/Redhat/Fedora 内存要求:≥64M 硬盘要求:2GB以上的剩余空间 服务器必须配置好软件源和可连接外网 必须具有系统 root 权限 建议使用干净 ...
- [问题2014A02] 解答一(两次升阶法,由张钧瑞同学、董麒麟同学提供)
[问题2014A02] 解答一(两次升阶法,由张钧瑞同学.董麒麟同学提供) 将原行列式 \(|A|\) 升阶,考虑如下 \(n+1\) 阶行列式: \[|B|=\begin{vmatrix} 1 &a ...
- SerialChat与Arduino的配合使用
最近在开发过程中,用到了Arduino开发板以及其IDE:Arduino,这个IDE使用起来很方便,编码也很简单,但有一点美中不足的是Arduino只能输出数值,不能绘图,所以就用到了另外一款串口调试 ...
- 关于autoptr
参考自: http://www.cppblog.com/expter/archive/2009/03/29/78270.html auto_ptr是什么. 解释1.auto_ptr是一个管理指针的对象 ...
- EasyUi 分页 和 表格数据加载
这里说明的是将说有数据先返回到前端再由前端去分页,性能可能没有先在后台分好页再返回给前端高 但如果操作不涉及大数据的话也没什么大问题,具体问题具体分析 要使用分页控件首先要声明初始化一下: //设置分 ...