0

In my universal application I have a master detail at the beginning. On the iPad it is just shown as a split view and when I change the interface orientation the autosizing works fine. But on the iPhone this doesn't work good enough in my detail view. So I want to have two different views for my DetailViewController that should have the exact same functionality, but I can't really get it to work properly. I tried to use two view controllers, but that didn't really do the job. I am using a storyboard by the way.

I thought I have found a solution by adding two views to my DetailViewController. Then I simply check the current orientation and set the recent view like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    Video *currentVideo = [[self.xmlParser videos] objectAtIndex:indexPath.row];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        [self.detailViewController setDetailItem:currentVideo];
    else {

        if (!self.detailViewController) {

            DetailViewController *nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
            if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait)
                [nextController setView:nextController.portraitView];
            else
                [nextController setView:nextController.landscapeView];
            [nextController setDetailItem:currentVideo];
            [self.navigationController pushViewController:nextController animated:YES];
        }        
    }
}

This works like it should, but now I face another problem. The two views contain exactly the same outlets, but I can only connect the outlets of either one of them to my DetailViewController class. So in one of the orientations I can't view my contents properly or I have to code everything twice what is exactly the thing I want to avoid.

Do have any ideas how I could handle this problem?

Thanks in advance

Bautzi89
  • 346
  • 1
  • 5
  • 21

1 Answers1

1

You can try adding two views inside a container view to the view controller in the Storyboard. Size one to be the portrait, the other - the landscape. Add all the controls you need to whichever view required. When the orientation changes - hide one and show the other.

Max Huk
  • 498
  • 3
  • 11
  • I tried that, but I couldn't really get it working. Do I have to keep a third view that is loaded initially and then add my custom views dependent on the orientation? – Bautzi89 Sep 13 '12 at 09:12
  • You can either put them both into a container view, or bind one of them to both the 'view' outlet and the 'portraitView' outlet on the controller (the other view will be bound to 'landscapeView' in this scenario). If you are not using a container view you will have to switch the view that's bound to the 'view' outlet of the controller when orientation changes instead of hiding/showing views. – Max Huk Sep 13 '12 at 10:43
  • Thanks for your quick answer, it helped me to move on. But now I'm facing new problems. Could you please check my edited question? – Bautzi89 Sep 13 '12 at 11:08
  • As for the two views - two sets of outlets problem, the same approach is required. If the outlet in question resides on the controller and references the view's content, make a generic outlet you will be referencing in all your logic, and two specialized ones for portrait / landscape modes. Switch which one of the specialized outlets is bound to the generic one on orientation change. As for the outlets that reside on the landscape / portrait views themselves - they can be bound to the same controller simultaneously, as only one of the views resides on the view stack (receives events). – Max Huk Sep 13 '12 at 18:48