project_files/HedgewarsMobile/Classes/SettingsContainerViewController.m
author koda
Thu, 09 Feb 2012 18:59:46 +0100
changeset 6658 2cccf6b2b89d
parent 6319 b57a37a94ad3
child 6672 4f728ccdd06b
permissions -rw-r--r--
added MGSplitViewController, popular replacement for uisplitviewcontrollers: this brings rotation support to our settings pages! weapons and schemes are the only controllers displaying minor glitches

/*
 * Hedgewars-iOS, a Hedgewars port for iOS devices
 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * File created on 27/03/2010.
 */


#import "SettingsContainerViewController.h"
#import "SettingsBaseViewController.h"
#import "MGSplitViewController.h"


@implementation SettingsContainerViewController
@synthesize baseController, splitViewRootController;

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return rotationManager(interfaceOrientation);
}


-(void) viewDidLoad {
    CGRect screenRect = [[UIScreen mainScreen] safeBounds];
    self.view.frame = screenRect;

    if (IS_IPAD()) {
        // the contents on the right of the splitview, setting targetController to nil to avoid creating the table
        SettingsBaseViewController *rightController = [[SettingsBaseViewController alloc] init];
        rightController.targetController = nil;
        UINavigationController *rightNavController = [[UINavigationController alloc] initWithRootViewController:rightController];
        [rightController release];

        // the contens on the left of the splitview, setting targetController that will receive push/pop actions
        SettingsBaseViewController *leftController = [[SettingsBaseViewController alloc] init];
        leftController.targetController = rightNavController.topViewController;
        UINavigationController *leftNavController = [[UINavigationController alloc] initWithRootViewController:leftController];
        [leftController release];

        self.splitViewRootController = [[MGSplitViewController alloc] init];
        self.splitViewRootController.delegate = nil;
        self.splitViewRootController.view.frame = screenRect;
        self.splitViewRootController.viewControllers = [NSArray arrayWithObjects: leftNavController, rightNavController, nil];
        self.splitViewRootController.showsMasterInPortrait = YES;
        [leftNavController release];
        [rightNavController release];

        // add view to main controller
        [self.view addSubview:self.splitViewRootController.view];
    } else {
        if (nil == self.baseController) {
            SettingsBaseViewController *sbvc = [[SettingsBaseViewController alloc] init];
            self.baseController = sbvc;
            [sbvc release];
        }
        self.baseController.targetController = nil;
        self.baseController.view.frame = CGRectMake(0, 0, screenRect.size.height, screenRect.size.width);

        [self.view addSubview:self.baseController.view];
    }

    [super viewDidLoad];
}

#pragma mark -
#pragma mark Memory management
-(void) didReceiveMemoryWarning {
    if (self.baseController.view.superview == nil)
        self.baseController = nil;
    if (self.splitViewRootController.view.superview == nil)
        self.splitViewRootController = nil;
    MSG_MEMCLEAN();
    [super didReceiveMemoryWarning];
}

-(void) viewDidUnload {
    self.baseController = nil;
    self.splitViewRootController = nil;
    MSG_DIDUNLOAD();
    [super viewDidUnload];
}

-(void) dealloc {
    releaseAndNil(baseController);
    releaseAndNil(splitViewRootController);
    [super dealloc];
}


#pragma mark -
#pragma mark view event management propagation
// every time we add a uiviewcontroller programmatically we need to take care of propgating such messages
// see http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/
-(void) viewWillAppear:(BOOL)animated {
    [self.splitViewRootController.detailViewController viewWillAppear:animated];
    [self.baseController viewWillAppear:animated];
    [super viewWillAppear:animated];
}

-(void) viewWillDisappear:(BOOL)animated {
    [self.splitViewRootController.detailViewController viewWillDisappear:animated];
    [self.baseController viewWillDisappear:animated];
    [super viewWillDisappear:animated];
}

-(void) viewDidAppear:(BOOL)animated {
    [self.splitViewRootController.detailViewController viewDidAppear:animated];
    [self.baseController viewDidAppear:animated];
    [super viewDidLoad];
}

-(void) viewDidDisappear:(BOOL)animated {
    [self.splitViewRootController.detailViewController viewDidDisappear:animated];
    [self.baseController viewDidDisappear:animated];
    [super viewDidUnload];
}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (IS_IPAD() == NO)
        return;

    CGRect screenRect = [[UIScreen mainScreen] safeBounds];
    self.splitViewRootController.masterViewController.view.frame = CGRectMake(0, 0, 320, screenRect.size.height);
}

@end