1
votes

I'm trying to create an iPad view with effectively 2 views. The left side view will be a menu/login area, the right side view will be for the content depending on which menu item is selected on the left.

I have added 2 Container Views and I have no trouble with creating the left side menu. However, I am having trouble telling the right side to switch between view controllers. How do you assign a UIViewController to a container view? I need to be able to switch out the right side with different view controllers when menu items are chosen.

Is this even possible? I don't want to use the Split View Controller as the right side is a UITable and I don't want a UITable.

If I'm not on the right track, can someone point me in the right direction?

Many thanks in advance.

1
Please mark the answer as accepted if it helped you.Mrunal

1 Answers

1
votes

Here is one sample code, which updates container view content for two separate buttons.

And contents are two different UIViewControllers.

Note: Before adding one view to ContinerView don't forget to clear container view to manage memory.

.h file

MyViewController1 * myViewController1;
MyViewController2 * myViewController2;

@property (nonatomic, strong) IBOutlet UIView *containerView;

.m file

// Button-1
- (IBAction)button1_TouchUpInside:(UIButton *)sender {

  for (UIView *view in [containerView subviews]) {

    [view removeFromSuperview];
  }

  [button1 setSelected:YES];

  myViewController1 = nil;

  myViewController1 = [[MyViewController1 alloc]
                           initWithNibName:@"MyViewController1"
                           bundle:[NSBundle mainBundle]];

  [self.containerView addSubView:myViewController1.view];
}

- (IBAction)button2_TouchUpInside:(UIButton *)sender {

  for (UIView *view in [containerView subviews]) {

    [view removeFromSuperview];
  }

  [button2 setSelected:YES];

  myViewController2 = nil;

  myViewController2 = [[MyViewController1 alloc]
                           initWithNibName:@"MyViewController1"
                           bundle:[NSBundle mainBundle]];

  [self.containerView addSubView:myViewController2.view];
}

Hope this will help to solve your problem.