(1)效果

(2)先在storyboard中设计界面,然后源码(直接在ViewController中码)

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,strong) NSArray *foods;
@property (weak, nonatomic) IBOutlet UITextField *fruitText;
@property (weak, nonatomic) IBOutlet UITextField *MainfoodText;
@property (weak, nonatomic) IBOutlet UITextField *BeverageText;
@property (strong,nonatomic) UIPickerView *foodPicker;
@property (weak, nonatomic) IBOutlet UITextField *dateText;
@property(strong,nonatomic) UIDatePicker *datePicker;
@end @implementation ViewController - (void)viewDidLoad {
_foodPicker=[[UIPickerView alloc]init];
_foodPicker.dataSource=self;
_foodPicker.delegate=self;
self.fruitText.inputView=_foodPicker;
self.MainfoodText.inputView=_foodPicker;
self.BeverageText.inputView=_foodPicker; UIToolbar *foodToolBar=[[UIToolbar alloc]init];
foodToolBar.barTintColor=[UIColor grayColor];
foodToolBar.frame=CGRectMake(0, 0, 320, 38);
UIBarButtonItem *randomBtn=[[UIBarButtonItem alloc]initWithTitle:@"随机" style:UIBarButtonItemStylePlain target:self action:@selector(randomPicker)];
[randomBtn setTintColor:[UIColor whiteColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"完毕" style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];
[doneBtn setTintColor:[UIColor whiteColor]];
UIBarButtonItem *spaceBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
foodToolBar.items=@[randomBtn,spaceBtn,doneBtn];
self.fruitText.inputAccessoryView=foodToolBar;
self.MainfoodText.inputAccessoryView=foodToolBar;
self.BeverageText.inputAccessoryView=foodToolBar; _datePicker=[[UIDatePicker alloc]init];
_datePicker.datePickerMode=UIDatePickerModeDate;
_datePicker.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
UIToolbar *dateToolBar=[[UIToolbar alloc]init];
UIBarButtonItem *doneBtn1=[[UIBarButtonItem alloc]initWithTitle:@"完毕" style:UIBarButtonItemStylePlain target:self action:@selector(dateDoneClick)];
dateToolBar.barTintColor=[UIColor grayColor];
dateToolBar.frame=CGRectMake(0, 0, 320, 38);
dateToolBar.items=@[spaceBtn,doneBtn1];
self.dateText.inputView=_datePicker;
self.dateText.inputAccessoryView=dateToolBar; [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} -(void)randomPicker{
for (int i=0; i<self.foods.count; i++) {
int row=arc4random()%[self.foods[i] count];
[self.foodPicker selectRow:row inComponent:i animated:YES];
[self pickerView:nil didSelectRow:row inComponent:i];
}
} -(void)doneClick{
[self.view endEditing:YES];
for (int i=0; i<self.foods.count; i++) {
[self pickerView:nil didSelectRow:[self.foodPicker selectedRowInComponent:i] inComponent:i];
}
} -(void)dateDoneClick{
[self.view endEditing:YES];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSString *newDate=[formatter stringFromDate:self.datePicker.date];
self.dateText.text=[NSString stringWithFormat:@"%@",newDate];
} -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return self.foods.count;
} -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.foods[component] count];
} - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return self.foods[component][row];
} -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component==0) {
self.fruitText.text=self.foods[component][row];
}else if (component==1){
self.MainfoodText.text=self.foods[component][row];
}else{
self.BeverageText.text=self.foods[component][row];
}
} -(NSArray *)foods{
if (_foods==nil) {
_foods=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]];
}
return _foods;
} @end

(3)总结

——这些选择器,基本都是结合输入框来使用的,即代替原本的键盘。所以我们的设置主要是用到了输入框的inputView属性。

——并且这些弹出的选择器一般上面会有一个UIToolBar,这里用到的是inputAccessoryView属性。

——并且这个UIToolBar里面放得都是UIBarButtonItem元素。我们能够自己定义也能够使用默认的,最重要的是UIBarButtonSystemItemFlexibleSpace用来对齐布局的。

——这里用的UIPickerView仅仅是显示字符串。而有的则须要显示更高级的东西,所以就须要用到view。而不是title。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

上面假设用到view作为UIPickerView内容,它也被设计成缓冲池回收机构。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

【iOS发展-70】点菜系统案例:使用文本框inputView和inputAccessoryView串联UIPickerView、UIDatePicker和UIToolBar的更多相关文章

  1. 【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识

    我们今天的情况下是第一个例子,下面的5一来通过切换页上一页下一页: (1)第一步,基本是以非常傻非常直接的方式来创建.这里用到的主要点有: --把对象变量设置为全局变量使得能够在其它方法中调用来设置它 ...

  2. IOS系统下虚拟键盘遮挡文本框问题的解决

    最近在项目中发现同样的代码在Android端微信网页中点击文本框唤出的虚拟键盘不会遮挡文本框,但是在IOS端的微信网页中点击文本框唤出的键盘却在大部分情况下会遮挡文本框 经过高人指点,这个问题终于解决 ...

  3. 机房收费系统总结之4——VB.NET 轻松解决判断文本框、组合框为空问题

    纵观机房收费系统,判断文本框.组合框为空问题无非两种情况.第一种:判断窗体中所有文本框.组合框是否为空.第二种:判断一部分文本框.组合框是否为空.下面看看是如何实现这两种情况的. 第一种:判断窗体中所 ...

  4. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

  5. JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)

    截取字符串: var str = "1234567890"; var a = str.substring(0,8);    //==str.substring(8)---结果:12 ...

  6. iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统

    这是一款在线图标转换工具,生成的图标支持Windows, Mac OS X, Linux, iOS, 和 Android等主流系统. 可以上传图标文件转化成另一个平台下的图标文件,例如将windows ...

  7. iOS 的 APP 在系统中如何适配不同的屏幕的尺寸

    iOS 的 APP 在系统中如何适配不同的屏幕的尺寸 标签: 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone ...

  8. iOS发展 ---- 至iPhone 6自适应布局设计 Auto Layout

    Apple从iOS 6增加了Auto Layout后開始就比較委婉的開始鼓舞.建议开发人员使用自适应布局,可是到眼下为止,我感觉大多数开发人员一直在回避这个问题,无论是不是因为历史原因造成的,至少他们 ...

  9. iOS发展 - 使用您自己的自定义字体

    一位同事问我最后一次,XXapp字体如何萌啊? 我也想提出萌哒哒的字体!然后,今天有这blog. 首先,我们正处于iOS发展,苹果给了我们很多的字体,当然,我就不一一列举在这里,英文,小汤表示看不懂啦 ...

随机推荐

  1. Winform WebBrowser引用IE版本问题

    做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...

  2. 《Linux Device Drivers》第十六章 块设备驱动程序——note

    基本介绍 块设备驱动程序通过主传动固定大小数据的随机访问设备 Linux核心Visual块设备作为基本设备和不同的字符设备类型 Linux块设备驱动程序接口,使块设备最大限度地发挥其效用.一个问题 一 ...

  3. 综合第一篇文章(带钩Quora)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDc4MzAyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. 【Android基础】单元测试的配置

    1.在AndroidManifest清单文件中进行配置 <application android:allowBackup="true" android:debuggable= ...

  5. Red Hat Enterprise Linux Server(RHEL) yum安装软件时This system is not registered with RHN. RHN support will be disabled. 的解决方法(转)

    新安装了redhat6.5.安装后,登录系统,使用yum update 更新系统.提示: This system is not registered to Red Hat Subscription M ...

  6. 【solr这四个主题】大约VelocityResponseWriter

    一个.大约Velocity基本配置 在Solr在,可以以多种方式返回搜索结果,作为一个简单的文字回复(XML.JSON.CSV等待),能够返回velocity.js等格式.而VelocityRespo ...

  7. centos下一个bash: XXX: command not found解决方案

    最近想centos通过做Android工程建设.配置jdk和Android sdk后,也同时/etc/profile将java和Android环境变量配置成,但它不能像windows 在相同,直接使用 ...

  8. js怎样推断一个对象{}是否为空对象,没有不论什么属性

    js怎样推断一个对象{}是否为空对象,没有不论什么属性 前段时间用js写了一个相似"angularjs"用于数据绑定的东西,功能是比較简单了, 通常应该传进来的是一个ArrayLi ...

  9. 示例:Netty 处理 TCP数据分包协议

    一个.Netty解决TCP协议的数据分包的想法 我们知道通过TCP协议发送接收数据时,假设数据过大.接收到的数据会是分包的.比方:                                   ...

  10. Mongodb语法总结

    mongodb与mysql指挥控制 由数据库中的一般传统的关系数据库(database).表(table).记录(record)三个层次概念组成.MongoDB是由数据库(database).集合(c ...