开发者

UIpickerview components in different UIViews, how to access?

How to access(get the user selection) from UIpickerview components in different UIViews (NOT using the Interface builder)?

Thanks.

Here is my code:

UIView *myView1 = [[UIView alloc] initWithFram开发者_如何学编程e:CGRectMake(0, 100, 100, 100)];
UIView *myView2 = [[UIView alloc] initWithFrame:CGRectMake(20, 250, 100, 100)];

[myView1 setBackgroundColor:[UIColor blueColor]];
[myView2 setBackgroundColor:[UIColor redColor]];

[self.view addSubview:myView1];
[self.view addSubview:myView2];

UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView1.delegate = self;
pickerView1.showsSelectionIndicator = YES;
[myView1 addSubview:pickerView1];

UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView2.delegate = self;
pickerView2.showsSelectionIndicator = YES;
[myView2 addSubview:pickerView2];

I have this callback, but once I put the pickerView in multiple UIViews it fails:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row); 
} 

This is the problem (but don't know the work around):

[self.view addSubview:pickerView1]; //<-- this works (able to get a response)
//[myView1 addSubview:pickerView1];  //<-- this fails 

Dont't know why I have to have it in the main view??


Since both pickerviews have the same delegate, you need to differentiate between them in the delegate methods. One way to do this is by using the tag property.

Set the tag property of each of your picker view's. Then in your UIPickerView delegate method pickerView:didSelectRow:inComponent:, query the tag and respond accordingly.

#define kVIEW1_PICKERVIEW_TAG 123
#define kVIEW2_PICKERVIEW_TAG 456

UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
UIView *myView2 = [[UIView alloc] initWithFrame:CGRectMake(20, 250, 100, 100)];

[myView1 setBackgroundColor:[UIColor blueColor]];
[myView2 setBackgroundColor:[UIColor redColor]];

[self.view addSubview:myView1];
[self.view addSubview:myView2];

UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView1.delegate = self;
pickerView1.tag = kVIEW1_PICKERVIEW_TAG;
pickerView1.showsSelectionIndicator = YES;
[myView1 addSubview:pickerView1];

UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView2.delegate = self;
pickerView2.tag = kVIEW2_PICKERVIEW_TAG;
pickerView2.showsSelectionIndicator = YES;
[myView2 addSubview:pickerView2];

-

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

switch( pickerView.tag ) {
    case kVIEW1_PICKERVIEW_TAG:
       /* code */
        break;
    case kVIEW2_PICKERVIEW_TAG:
        /* code */
        break;
    default:
        break;
    }

}


@jdl you need to read UIPickerView's delgate methods. And also delegate protocols.


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

 }


You can use the delegate method pickerView:didSelectRow:inComponent: in UIPickerViewDelegate Protocol and use the tags to identify the picker

assign tags:

pickerView1.tag = 1;
pickerView2.tag = 2;

In your delegate method:

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

if(thePickerView.tag == 1 ){
//do this
}

else if (thePickerView.tag == 2){
//do this 
}

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜