ios - IOS 5 应用程序中的多个 UIPickerView

标签 ios xcode uitextfield storyboard uipickerview

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

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

用户应点击“国家/地区”输入字段,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 文件中,我有综合属性,在 Storyboard中,我只有 3 个输入字段已连接到适当的 socket 。 我应该注意现有代码中的一些基本错误吗?

现在,我觉得我在阅读有关选择器 View 的教程时错过了一些东西,因为我在 StackOverflow 上找到的大多数示例对我来说意义不大。

有人可以给我指出一个可以帮助我的基本类似示例。 UIPickers 是要走的路还是有更好的方法?

我已经有一段时间没有感到这种无助了,任何帮助将不胜感激,谢谢

编辑:

好的,我正在取得进步,我希望我能帮助遇到同样问题的其他人。 要使其工作并将 pickerview 连接到输入字段的 inputView 属性,您需要执行此操作。

您需要在 .h 文件中添加两个委托(delegate):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 委托(delegate)方法,这些方法将使用正确的数据填充 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];
}

我希望这可以帮助某人。

最佳答案

您好,请遵循多重选择器 View 的整个解决方案,或者只使用 -(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;

}

}

关于ios - IOS 5 应用程序中的多个 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10722918/

相关文章:

ios - viewcontrller 中的应用程序委托(delegate)功能 - Facebook

ios - 需要有关自动布局的帮助

ios - NSAttributedString 不呈现 html 换行符

ios - 注释引脚作为用户位置

ios - 设置一个类 IBOutlet 引用另一类文本字段的导出?

iphone - 当显示 UIViewController 时如何使用键盘来编辑字段?

ios - 粘贴到 UITextField 时去除空格?

ios - 在 tableView 中显示数组

ios - 与启动图像完全一样的启动屏幕

ios - UITextField 与 PickerView : Don't allow editing