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> ...
随机推荐
- iftop命令命令详解
iftop命令命令详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在Linux命令中有很多内置命令,和外置命令,但是内部命令的功能毕竟是有限的,比如ifconfig,它就不能看 ...
- Java关于链表的增加、删除、获取长度、打印数值的实现
package com.shb.java; public class Demo8 { public Node headNode = null; /** * @param args * @date 20 ...
- Windows10 如何删掉内置的 skype ?
打开开始菜单,输入“PowerShell”并回车: 运行“Get-AppxPackage -User username”命令( username 请替换成当前实际用户名),此时会显示所有已安装的应用程 ...
- Myeclipse8.5 最新注册码以使用方法(可以用到2015年!!!)
已破解的一组,复制即可!(注册码到2015年哦!) name:LIKEcode:YLR8ZC-855550-6067725176540043 使用方法:把注册码贴到Window-->prefer ...
- SVN服务端启动解决方案(2013-12-10 记)
解决每一次开机都得用DOS启动SVN服务,而DOS窗口又无法关闭的情况 1.安装Setup-Subversion-1.8.5.msi搭建好SVN服务端(下载地址:http://subversion. ...
- Upload file
<h3>Upload File</h3> <form action="@Url.Action("Upload","UploadCo ...
- TCP/IP学习-链路层
链路层: 路径MTU: 网络层: ifconfig netstat IP首部 网络字节序:大端字节序
- Dynamics AX 2012 R2 RemoteApp导出项目报错
今天,Reinhard使用RemoteApp的方式登陆AX开发环境,对项目文件进行修改后,习惯性地将项目导出到Reinhard的电脑上,做个备份.但是导出时弹出错误提示框,报以下错误: ...
- flexigrid随手记
最近要用到flexigrid做表格,随手记一些知识点. 引入了两个jquery库(jquery.js和jquery-1.7.1.min.js),发生冲突,只保留一个 $("#gridTabl ...
- [课程设计]Scrum 1.6 多鱼点餐系统开发进度(点餐页面按钮添加&修复)
[课程设计]Scrum 1.6 多鱼点餐系统开发进度(点餐页面按钮添加&修复) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4. ...