I have created a ViewController containing one picker. Its view is triggered to appear and populate the picker with one of four data arrays, depending on which one of four buttons has been pressed in the superview. I gave the subview an int property called pickerType, which I then set within the button code, like so:
- (IBAction)chooseDepType:(id)sender
{
if (pickerView==nil)
{
pickerView = [[Picker alloc] initWithNibName:@"Picker" bundle:nil];
//send message to next view telling which array to load in picker
pickerView.pickerType=1;
}
[self.view addSubview:pickerView.view];
}
The other button methods look the same, except that the pickerType is set to a different value.
Then, in the subview, I set up the datasource and delegate methods like so:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
if(pickerType_ ==1)
{
// return the appropriate number of components, for instance
return 1;
}
}
The problem I have is that even though I am setting pickerView to a new value in each button’s code, my subview is not recognizing that the value has changed. Basically, no matter which button I press, it shows me a picker filled with the data corresponding to the first button I pressed. To complicate matters, perhaps, the superview is a Detail view in a Master-Detail template.
pickerType property is created like this:
@property (assign) int pickerType;
@synthesize pickerType=pickerType_;
I am using ARC. I don’t know if this is a dumb programming error on my part, or has something to do with the lifecycle of a view.
Since you are checking whether your
Pickerhas already been created:before setting the type:
the next time you press a button,
pickerViewalready exists, execution skips over the body of theifand the type is not changed.To fix this, move the type-setting statement out of the
ifblock.Incidentally, if you have four buttons that all do almost exactly the same thing, you can have them all trigger the same
IBActionand distinguish which was pressed by theirtagproperties: