开发者

Using more than one UIPickerView

I decided to use a UIPickerView to simulate a kind of dropdown menu. My problem is, that i want to use more than one UIPickerView. How do i manage to set up a second UIPickerView? Adding a new UIViewController? or how does it work? Adding a second span won't be enough, i need up to 3 more UIPickerViews. Thanks in advance :)

Code:

   -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 1;
    }

    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return [pickerarray count];
    }

    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        return [pickerarray objectAtIndex:row];
    }

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    }

- (void)viewDidLoad {

pickerarray = [[NSMutableArray alloc] init];
[pickerarray addObject:@"Hamburg"];
[pickerarray addObject:@"Berlin"];
[pickerarray addObject:@"München"];
[pickerarray addObject:@"Bonn"];

pickerarray2 = [[NSMutableArray alloc] init];
[pickerarray2 addObject:@"Hambburg"];
开发者_开发问答[pickerarray2 addObject:@"Berelin"];
[pickerarray2 addObject:@"Müneechen"];
[pickerarray2 addObject:@"Boneen"];
}


  1. Add all of them into your view.
  2. Set them as hidden.
  3. Set unique tags for each of them.
  4. Add buttons that "reveal them".
  5. Implement delegate methods that manage values depending on picker view tag.

Code update:

// put this before view controllers @implementation
enum {
  MyFirstPickerViewTag = 1,
  MySecondPickerViewTag
} MyPickerViewTags; // fix for warning

// assign tags 1 and 2 respectively to your picker views in IB
// here's one of delegate methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    switch(pickerView.tag) {
        case MyFirstPickerViewTag: {
            return 2;
        }
        case MySecondPickerViewTag: {
            return 3;
        }
    }
    return 1; //  component by default
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜