1

I'm new to Xcode and starting a universal app. In the iPad version I want to re-use the main section for different search forms.

I want the user to see a completely different form in the section to the right (see image) for each button on the left menu.

I'm working programmatically.

enter image description here

What's the best way to change the content in this situation? (keeping in mind performance and coding complexity)

EDIT: This is a standard tabbed application, not a split view. Could I have a new view controller and .xib for each form and then change which one is displayed by embedding them in a UIView or UIWindow on this screen?

PaulMrG
  • 1,822
  • 2
  • 16
  • 20

1 Answers1

2

You've hit upon the right answer in your edit. From what I understand the buttons on the side are analagous to another tab bar controller, but nested within your main tab bar controller?

You might as well follow the same pattern, since it is a familiar one. Your search view controller should act as a container view controller, which would have an array of view controllers representing each form option.

As you switch between the options, add/remove the appropriate view controller views from your view hierarchy (using addSubview) and view controller hierarchy (using the code in "Adding and removing a child" in the link). The view controller hierarchy is important to ensure that viewDidAppear and so forth is called on your child controllers.

As an illustration, I've created a simple demo project where the main view controller has a set of buttons, each linked to the same action, and an array of contained view controllers. The contained view controllers will be held inside a subview, called container in this example. This would be the size of the right hand area in your screenshot above.

The child controllers are set up as follows in viewDidLoad of the container view controller:

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
@property (weak, nonatomic) IBOutlet UIView *container;

@property (nonatomic,strong) NSArray *viewControllers;
@property (nonatomic, strong) UIViewController *currentChild;

@end

@implementation JRTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    for (UIButton *button in self.buttons)
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    NSMutableArray *children = [NSMutableArray new];

    for (int i = 0; i < 4; i++)
    {
        ContainedViewController *child = [ContainedViewController new];
        child.name = [NSString stringWithFormat:@"Form %d",i + 1];
        [children addObject:child];
    }

    self.viewControllers = [children copy];
    [self buttonTapped:self.buttons[0]];
}

Here I've used four instances of the same view controller class - all it has is a label which indicates which form you are selecting. Really you'd have different classes for each one. I've also "selected" the initial view controller by sending the action method for the first button. The action method does this:

- (IBAction)buttonTapped:(UIButton *)sender
{
    NSUInteger index = [self.buttons indexOfObject:sender];
    if (index != NSNotFound)
        self.currentChild = self.viewControllers[index];
}

Which selects the appropriate VC from the view controller array. The setter method does this:

-(void)setCurrentChild:(UIViewController *)currentChild
{
    if (currentChild == _currentChild) return;

    // Remove the old child
    [_currentChild willMoveToParentViewController:nil];
    [_currentChild.view removeFromSuperview];
    [_currentChild removeFromParentViewController];
    [_currentChild didMoveToParentViewController:nil];

    // Add the new one
    [currentChild willMoveToParentViewController:self];
    [self.container addSubview:currentChild.view];
    NSDictionary *views = @{@"view":currentChild.view};
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[view]|" options:0 metrics:nil views:views]];
    [self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:views]];
    [self addChildViewController:currentChild];
    [currentChild didMoveToParentViewController:self];
    _currentChild = currentChild;
}

Which sets up the view controller hierarchy, adds in the view and uses constraints to make it fill the container.

In this example I've hardcoded in four buttons and four child view controllers as I was demonstrating the addition and switching of child view controllers. In reality you'd make it more like a tab bar controller where you assign an array of view controllers and the container would make its own array of buttons.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • I think your link to "container view controller" is a step in the right direction. It's proving difficult to find things in the iOS docs when you don't know what to search for. I'll read through it now. – PaulMrG Feb 17 '13 at 10:22
  • I understand the concept but could do with some help with the implementation. Are there any tutorials for this? Or is it easy enough to show a simple example here? – PaulMrG Feb 17 '13 at 15:44
  • I'll put an example on here later on tonight. If there's anything in particular you don't get, please edit your question or add a comment and I'll make sure I cover it. – jrturton Feb 17 '13 at 16:49
  • Thanks @jrturton. I made a new project to test this. I made a new class in it called ContainedViewController with .h .m and .xib. added a property in the .h of NSString *name. On the FirstViewController.xib I added a UIView and made a referencing outlet to container. When I run it I can see a label I added to the ContainedViewController.xib which is good. Now, I added four buttons to FirstViewController.xib and connected them all to the IBAction buttonTapped. Now to add more views do I create some more view controllers or views and connect them to ContainedViewController? – PaulMrG Feb 18 '13 at 03:51
  • Ok so I removed the for loop that adds buttons and the other one that adds the children although I kept the inside of it, then copied it and changed the class names to add separate views to the children array according to new view controllers I created e.g. ContainedViewController2 and 3 etc. Then I made a separate IBAction for each of my buttons with just the one line `self.currentChild = self.viewControllers[0];`. It's working now but I had some issues with the constraints. I commented out those two lines and it works as expected without the errors. – PaulMrG Feb 18 '13 at 06:12
  • Shouldn't have commented out the constraints because it didn't allow any auto re-sizing of the children. I used @David Wong's answer on [this question](http://stackoverflow.com/questions/11664115/getting-weird-error-talking-about-constraints-in-xcode) and added `[currentChild.view setTranslatesAutoresizingMaskIntoConstraints:NO];` before the other constraints, now it's all good. – PaulMrG Feb 18 '13 at 06:55
  • Yes, I didn't show the code for the contained controllers because it didn't really contain much - but it did turn off the translatesAutoResizing property! Sorry for the omission. – jrturton Feb 18 '13 at 07:18