English 中文(简体)
IOS5 应用程序中的多个 UIPicker 视图
原标题:Multiple UIPickerViews in IOS5 app

我两个月前开始学习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];
}

我希望这能帮到人

最佳回答

Hi 请遵循多个拾取器视图的完整解决方案, 或只使用 < 强 > -( 避免) 创建动作表 < / 强 >, 它可以解决您的问题

<强度 > deafine in.h

UIActionSheet *actionSheet;
NSString *pickerType;
BOOL select

<强度 > define in.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;

}

}
问题回答

UIPickers 可以使用, 但在此特定情况下, 条目列表可以是非常长的列表, 而条目也可以有一定的时间, 可能不仅仅是一个简单的字符串, 例如, 如果国家名称是“ 津巴布韦共和国, 非洲” 或者类似的东西, UIPicker 控制器无法以最佳方式显示数据 。 您可以使用表格视图, 并以模式方式展示它们。 如果条目列表超过 100 个左右, 表格视图可以给您更好的选择方法 。





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签