0

I have a simple app that uses a UITabBar controller for navigation. Let's say I have ViewA and ViewB on the tabBar. Everything works fine. However I also want to add a UIButton to ViewA that will present the user with ViewB when pressed.

In other words there are two ways to get to ViewB from ViewA. The first is from the tabBar and the second is by pressing the button in ViewA.

What would be the best way of accomplishing this. Thanks.

SNV7
  • 2,563
  • 5
  • 25
  • 37
  • Why don't you use a tabBarController ? – KIDdAe Mar 27 '13 at 17:08
  • My apologies, I meant that I'm already using a tabBar controller. Just edited my question for more clarity, – SNV7 Mar 27 '13 at 17:14
  • 1
    possible duplicate of [UITabBarController is possible to select the tab via code?](http://stackoverflow.com/questions/833389/uitabbarcontroller-is-possible-to-select-the-tab-via-code) – Björn Kaiser Mar 27 '13 at 17:28

1 Answers1

0

Ok then I would define a protocol in ViewA.h

@protocol ViewADelegate
-(void)viewAPressButton;
@end

and add a property to ViewA :

@property (nonatomic, assign) id<ViewADelegate> delegate

don't forget to set this property when instantiate this controller.

call this method when the button is pressed in ViewA.m

-(IBAction)buttonPressed:(id)sender {
[delegate viewAPressButton];
}

then implement in the correct place (maybe AppDelegate in your case ?) Assuming ViewB is at index 1

-(void)ViewAPressButton {
[self.tabBarController setSelectedIndex:1];
}
KIDdAe
  • 2,714
  • 2
  • 22
  • 29