我大约两个月前开始学习iOS编程,到目前为止我很喜欢它,但我在UIPicker(s)和UIDatePickers中有点挣扎在我正在制作的应用程序中,我希望有人可能会指出我正确的方向。该应用程序应该模仿已经存在的PHP Web应用程序,这只不过是一个包含大量下拉菜单的表单。
假设我有GetAddressViewController,它将包含一堆输入字段。为简单起见,我可以说我只有3个输入字段:country,city和street。
用户应点击“国家/地区”输入字段,UIPicker会像键盘一样显示,选择国家/地区后,网络应用程序将返回该国家/地区所有城市的JSON数组。当用户点击“城市”输入字段时,重复相同的过程,UIPicker弹出一个返回城市列表,用户选择一条街道,UIPicker滑出,网络应用程序返回一系列街道等。
让我们说在我的GetAddressViewController.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
在GetAddressViewController.m文件中我已经合成了属性,在故事板中我只有3个输入字段已经连接到适当的出口。 在我现有的代码中是否存在一些我应该注意的根本错误?
现在,我觉得在阅读有关选择器视图的教程时我遗漏了一些内容,因为我在StackOverflow上找到的大多数示例对我来说都没有多大意义。
有人能指出一个可以帮助我的基本类似例子。 UIPickers是否可行?还是有更好的方法?
我暂时没有感到这种无助,非常感谢任何帮助,谢谢
修改
好的我正在取得进步我希望我可以帮助其他有同样问题的人。 要使其工作并将pickerview连接到输入fileds inputView属性,您需要执行此操作。
对于.h文件,您需要添加两个委托:UIPickerViewDelegate和UIPickerViewDataSource
@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>{ ...
然后在你的.m文件中,你需要有这样的东西。我的wiewDidLoad方法中有这段代码。
UIPickerView *cityPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
cityPicker.delegate = self;
cityPicker.dataSource = self;
[cityPicker setShowsSelectionIndicator:YES];
cityField.inputView = cityPicker;
这只是意味着当您点击cityField时将显示cityPicker选择器视图。之后您需要添加以下pickerview委托方法,这些方法将为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;
添加一个新方法pickerDoneClicked
-(void)pickerDoneClicked
{
NSLog(@"Done Clicked");
[cityField resignFirstResponder];
}
我希望这可以帮助别人。
答案 0 :(得分:2)
您好请遵循多个pickerview的整个解决方案或只使用 - (void)createActionsheet 它可以解决您的问题
在.h
中定义UIActionSheet *actionSheet;
NSString *pickerType;
BOOL select
在.m中定义
-(IBAction)countryBtnPressed:(id)sender
{
[self createActionSheet];
pickerType = @"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
Txt.text = [array objectAtIndex:0];
[chPicker release];
}
-(IBAction)stateBtnPressed:(id)sender
{
[self createActionSheet];
pickerType = @"statepicker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
stateTxt.text = [stateArray objectAtIndex:0];
[chPicker release];
}
/// Create Action Sheet
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:@"picker"])
count = [array count];
else if([pickerType isEqualToString:@"picker"])
count = [statearray count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:@"picker"])
string = [array objectAtIndex:row];
if ([pickerType isEqualToString:@"statepicker"])
string = [statearray objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:@"picker"])
{
Txt.text = [array objectAtIndex:row];
}
if ([pickerType isEqualToString:@"statepicker"])
{
stateTxt.text = [statearray objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:@"picker"])
{
Txt.text = [array objectAtIndex:0];
}
else if ([pickerType isEqualToString:@"statepicker"])
{
stateTxt.text = [statearray objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}
答案 1 :(得分:1)
UIPickers可以使用,但在这种特殊情况下,条目列表可以是一个非常长的列表,并且条目有时可能不仅仅是一个简单的字符串,例如,如果国家名称是“津巴布韦共和国,非洲“或UIPicker控制器之类的东西无法以最佳方式显示数据。您可以使用表视图并以模态方式显示它们。如果条目列表超过一百个,Tableviews会为您提供更好的选择方法。