Popup/modal picker for a text field in iOS 7(iOS 7中文本字段的弹出/模式选择器)
问题描述
I am having a lot of trouble figuring out how to implement a standard popup picker. Like many apps' registration screen when a user selects the birthday text field I'd like a popup picker to appear so that users can select their birthday, click done and the formatted date will be added to the text field. This doesn't seem like it should be all that hard, yet it seems there is no simple, clear, standard way of doing this in iOS 7.
I've searched the internet and seen some saying to use modals, others say actionsheets, others say popups and still others say a separate view controller.
Can anyone tell me what the standard way of doing this is or a snippet on how to implement it?
I think the "standard" way, is to set the picker as the inputView of the text field.
UIPickerView *picker = [[UIPickerView alloc] init];
self.textField.inputView = picker;
It will pop up front the bottom, just like the keyboard does when you touch in the text field.
Here's a simple implementation of how to use a picker as an input view:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (strong,nonatomic) NSArray *theData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.dataSource = self;
picker.delegate = self;
self.tf.inputView = picker;
self.theData = @[@"one",@"two",@"three",@"four"];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.theData.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.theData[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.tf.text = self.theData[row];
[self.tf resignFirstResponder];
}
这篇关于iOS 7中文本字段的弹出/模式选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 7中文本字段的弹出/模式选择器
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
