多个UIPickers显示在操作表中

时间:2012-01-10 20:25:17

标签: objective-c uipickerview

在我的一个视图控制器中,我想使用多个选择器。

标题文件:

@interface MyTableController : TTTableViewController <UIActionSheetDelegate, UIPickerViewDataSource, UIPickerViewDelegate>{

    IBOutlet UIPickerView *picker1;
    IBOutlet UIPickerView *picker2;

    NSMutableArray *list1;
    NSMutableArray *list2;
}

@property(nonatomic,retain) UIPickerView *picker1, *picker2;

-(IBAction)togglePickerView1;
-(IBAction)togglePickerView2;

@end

实施档案:

@implementation MyTableController

@synthesize picker1, picker2;

int row_index1 = 0;
int row_index2 = 0;

- (void)locationPicker:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
    if([pickerView isEqual: picker1]){
        row_index1 = row;
    }

    if([pickerView isEqual: picker2]){
        row_index2 = row;
    }
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    if([pickerView isEqual: picker1]){
        return 1;
    }

    if([pickerView isEqual: picker2]){
        return 1;
    }         
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if([pickerView isEqual: picker1]){
        return [list1 count];
    }

    if([pickerView isEqual: picker2]){
        return [list2 count];
    }  
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{
    return [list objectAtIndex:row];
    if([pickerView isEqual: picker1]){
        return [list1 objectAtIndex:row];
    }

    if([pickerView isEqual: picker2]){
        return [list2 objectAtIndex:row];
    }

    return nil;
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {  

}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {

    if (actionSheet.tag == 111) {
        picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
        picker1.showsSelectionIndicator = YES;
        picker1.dataSource = self;
        picker1.delegate = self;  

        //Add picker to action sheet
        [actionSheet addSubview:picker1];
        [picker1 release];
    }else if(actionSheet.tag == 222){
        picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
        picker2.showsSelectionIndicator = YES;
        picker2.dataSource = self;
        picker2.delegate = self;

        //Add picker to action sheet
        [actionSheet addSubview:picker2];
        [picker2 release];
    }

    //Gets an array af all of the subviews of our actionSheet
    NSArray *subviews = [actionSheet subviews];

    [[subviews objectAtIndex:1] setFrame:CGRectMake(20, 266, 280, 46)]; 
    [[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}

-(IBAction)togglePickerView1{
    UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"FLT", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"CANCEL", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"PICK", nil), nil];
    [asheet setTag:111];
    [asheet showInView:[self.view superview]]; //note: in most cases this would be just self.view, but because I was doing this in a tabBar Application, I use the superview.
    [asheet setFrame:CGRectMake(0, 117, 320, 383)];
    [asheet release];   
}

-(IBAction)togglePickerView2{
    //...  
    [asheet setTag:222];
    //...
}

- (void)loadView {
    [super loadView];
}

-(void)viewDidLoad{

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Button1" style:UIBarButtonItemStyleBordered target:self action:@selector(togglePickerView1)];

    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStyleBordered target:self action:@selector(togglePickerView2)];


    NSArray *myToolbarItems = [[NSArray alloc] initWithObjects: item1, item2, nil];                 

    [self setToolbarItems: myToolbarItems];
    [myToolbarItems release];

    list1 = [[NSMutableArray alloc] init];

    [list1 addObject:@"--"];
    [list1 addObject:@"Test1"];

    list2 = [[NSMutableArray alloc] init];

    [list2 addObject:@"--"];
    [list2 addObject:@"Test2"];
}


@end

我的问题是无论我按哪个按钮,它总是被触发的picker1。问题出在哪里?

1 个答案:

答案 0 :(得分:2)

您正在成功创建两个不同的选择器,并且每次都显示正确的选择器。

问题是,每个选择器都有相同的数据。

数据源titleForRow...方法的第一行是:

return [list objectAtIndex:row];

这会通过返回值来结束数据源方法的执行,因此无论代码的其余部分如何,两个选择器都将始终显示相同的数据。上面的代码中没有声明list,所以我不确定你在屏幕上看到了什么。

我使用上面的代码构建了一个示例项目,并确认这是问题所在。删除该行会为您提供两个不同的选择器,每个选择器具有不同的内容。