我两个月前开始学习iOS编程,至今我都很喜欢它,但我有点与 UIPicker 和 UIDatePickers 在我制作的应用程序中挣扎,我希望有人能指向我正确的方向。该应用程序应该模仿已有的 PHP 网络应用程序,这只不过是一堆垃圾的表格。
假设我有GetAddress View Control, 它将拥有一堆输入字段。 为了简单起见, 可以说我只有3个输入字段: 国家、 城市和街道 。
用户应该使用“ 国家” 输入字段, UIPicker 显示的类似于键盘键盘, 在选择国家后, 一个网络应用程序将返回该国所有城市的 JSON 阵列。 当用户在“ 城市” 输入字段上点击时, UIPicker 跳出一个返回的城市列表, 用户选择一条街道, UIPicker 滑出, 网络应用程序返回一系列街道等, 同样的过程会重复 。
这么说吧,在我的GetAddress View Constor.h档案里,我有:
#import <UIKit/UIKit.h>
@interface GetAddressViewController : UIViewController
{
UITextField *countryField;
UITextField *cityField;
UITextField *streetField;
NSArray *countries;
NSArray *cities;
NSArray *streets;
}
@property(nonatomic, strong) IBOutlet UITextField *countryField;
@property(nonatomic, strong) IBOutlet UITextField *cityField;
@property(nonatomic, strong) IBOutlet UITextField *streetField;
@property(nonatomic, strong) IBOutlet NSArray *countries;
@property(nonatomic, strong) IBOutlet NSArray *cities;
@property(nonatomic, strong) IBOutlet NSArray *streets;
@end
In GetAddressViewController.m file i have synthesized properties and in the storyboard i only have 3 input fields that have been connected to appropriate outlets. Is there some fundamental mistake in my existing code that i should be aware of?
现在,我觉得我在阅读关于拾取者观点的教程时遗漏了一些东西,因为我在StackOverflow上发现的大多数例子对我来说没有意义。
有人能指给我一个可以帮助我的基本类似的例子吗? UIPickers是走这条路还是有更好的方法?
我很久没有感到如此无助了 任何帮助都会非常感激,谢谢
<强度 > EDIT:
Ok I m making progress I hope that I can help someone else who has the same problem. To make this work and to connect pickerview to input fileds inputView property you need to do this.
您需要将两个代表添加到您的.h 文件中 : UIPicker ViewDelegate 和 UIPickerViewData 源
@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>{ ...
在你的.m档案里,你需要这样的东西。 我有这个代码,在我的WiewDed Load方法中。
UIPickerView *cityPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
cityPicker.delegate = self;
cityPicker.dataSource = self;
[cityPicker setShowsSelectionIndicator:YES];
cityField.inputView = cityPicker;
这简单意味着当您点击城市字段时, 城市Picker Pickerview 将会出现。 之后您需要添加以下的拾取者代表方法, 以正确的数据填充拾取者视图 。 我的例子中的数据是一个包含城市列表的阵列 。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return cities.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [cities objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
cityField.text = (NSString *)[cities objectAtIndex:row];
}
如果您想要隐藏 UIPicker 的“ 完成”, 您需要这个代码 :
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
cityField.inputAccessoryView = mypickerToolbar;
并添加新的方法拾取器 DoneClclced
-(void)pickerDoneClicked
{
NSLog(@"Done Clicked");
[cityField resignFirstResponder];
}
我希望这能帮到人