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] 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.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.–