I have two views for my app depending on what orientation it decides which view to load. But IB wont allow me to connect two PickerViews to the same OUTLET, is there a way t
I recently applied this concept in my application. What you can do in such a case is to assign an int to each button which displays the picker on touch.
Say you have two buttons btn1 and btn2. Let their touch actions are btn1Action and btn2Action. You can also have a single toolbar for both pickers. Set visibility of picker and toolbar as hidden in xib.
-(void)btn1Action:(id)sender
{
picker.setHidden = NO;
toolbar.setHidden = NO;
iPicker = 1; // Declared globally
}
-(void)btn2Action:(id)sender
{
picker.setHidden = NO;
toolbar.setHidden = NO;
iPicker = 2; // Declared globally
}
-(void)toolbarAction:(id)sender
{
picker.setHidden = YES;
toolbar.setHidden = YES;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(iPicker==1)
{
return 10;
}
if(iPicker == 2)
{
return 8;
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)iComponent
{
if(iPicker==1)
{
// Your code
}
else if(iPicker == 2)
{
// Your code
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)iComponent
{
else if(iPicker == 1)
{
// Your code
}
else if (iPicker == 2)
{
// Your code
}
}
Hope this helps.
Well, bear in mind that an IBOutlet is simply a property that has been declared in a way that makes it visible in IB. So the answer to your first question is yes. You can always reassign that property in code if necessary.
I presume you already have two IBOutlets for your landscape and portrait views -- something like this:
@property (nonatomic, retain) IBOutlet UIView *landscapeView;
@property (nonatomic, retain) IBOutlet UIView *portraitView;
And it sounds like you're selecting the appropriate view in willAnimateRotationToInterfaceOrientation:duration:
.
Similarly you could declare two outlets for your picker views:
@property (nonatomic, retain) IBOutlet UIPickerView *landscapePickerView;
@property (nonatomic, retain) IBOutlet UIPickerView *portraitPickerView;
If you go this route, I'd declare a dynamic property that always returns the picker view for the current orientation.
@property (nonatomic, retain, readonly) UIPickerView *pickerView;
Rather than synthesizing this property, you could implement it like this:
- (UIPickerView *) pickerView {
if (self.landscapeView.superview) {
return self.landscapePickerView;
}
else {
return self.portraitPickerView;
}
}
However, if you have more than one or two subviews, having parallel properties like this cluttering your controller could get to be a pain. In that case, I'd consider making a custom subclass of UIView called something like PickerContainer that has outlets for your pickerView and any other subviews you need to access. Then in IB you can change the classes of your landscape and portrait views to PickerContainer, and you can connect each picker directly to its superview. Then in your controller you can just create one dynamic property like this:
@property (nonatomic, retain, readonly) PickerContainer *pickerContainer;
- (PickerContainer *)pickerContainer {
return (PickerContainer *)self.view;
}
and after that you can access your pickerView for the current orientation via its container like this:
[self.pickerContainer.pickerView reloadAllComponents];
EDIT: Here's how I implemented willAnimateRotationToInterfaceOrientation:duration:
on one of my projects:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
if (self.landscapeView.superview) [self.landscapeView removeFromSuperview];
self.portraitView.center = CGPointMake(self.view.bounds.size.width / 2,
self.view.bounds.size.height / 2);
[self.view addSubview:self.portraitView];
}
else {
if (self.portraitView.superview) [self.portraitView removeFromSuperview];
self.landscapeView.center = CGPointMake(self.view.bounds.size.width / 2,
self.view.bounds.size.height / 2);
[self.view addSubview:self.landscapeView];
}
}
My landscape and portrait views were configured with no struts or springs in IB, meaning all the margins are flexible, but the width and height aren't.