0

我大约两个月前开始学习 iOS 编程,到目前为止我很喜欢它,但是我在我正在制作的应用程序中使用 UIPicker(s) 和 UIDatePickers 有点挣扎,我希望有人可以指出我正确的方向。该应用程序应该模仿已经存在的 PHP Web 应用程序,这只不过是一个带有一堆下拉菜单的表单。

假设我有 GetAddressViewController,它将有一堆输入字段。为了简单起见,假设我将只有 3 个输入字段:国家、城市和街道。

用户应该点击“国家”输入字段,UIPicker 会像键盘一样显示出来,在选择国家后,Web 应用程序将返回一个包含该国家所有城市的 JSON 数组。当用户点击“城市”输入字段时重复相同的过程,UIPicker 弹出返回城市列表,用户选择一条街道,UIPicker 滑出,Web 应用程序返回一系列街道等。

可以说,在我的 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 连接到输入字段 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 委托方法,这些方法将使用正确的数据填充 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];
}

我希望这可以帮助某人。

4

2 回答 2

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;

}

}
于 2012-05-24T09:28:46.477 回答
1

UIPickers 可以使用,但在这种特殊情况下,条目列表可能是一个很长的列表,并且条目有时可能不仅仅是一个简单的字符串,例如,如果国家名称是“津巴布韦共和国” , Africa" 或类似的 UIPicker 控制器无法以最佳方式显示数据。您可以使用表格视图并以模态方式呈现它们。如果条目列表超过一百个左右,表格视图为您提供了一种更好的选择方式。

于 2012-05-23T16:11:18.117 回答