project_files/HedgewarsMobile/Classes/SettingsContainerViewController.m
branchios-revival
changeset 11217 e68b3e392091
parent 11216 76fd61a88c1e
child 11218 08f20ffa5801
equal deleted inserted replaced
11216:76fd61a88c1e 11217:e68b3e392091
     1 /*
       
     2  * Hedgewars-iOS, a Hedgewars port for iOS devices
       
     3  * Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
       
    17  */
       
    18 
       
    19 
       
    20 #import "SettingsContainerViewController.h"
       
    21 #import "SettingsBaseViewController.h"
       
    22 #import "MGSplitViewController.h"
       
    23 
       
    24 
       
    25 @implementation SettingsContainerViewController
       
    26 @synthesize baseController, splitViewRootController;
       
    27 
       
    28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    29     return rotationManager(interfaceOrientation);
       
    30 }
       
    31 
       
    32 -(void) viewDidLoad {
       
    33     CGRect screenRect = [[UIScreen mainScreen] safeBounds];
       
    34     self.view.frame = screenRect;
       
    35 
       
    36     if (IS_IPAD()) {
       
    37         // the contents on the right of the splitview, setting targetController to nil to avoid creating the table
       
    38         SettingsBaseViewController *rightController = [[SettingsBaseViewController alloc] init];
       
    39         rightController.targetController = nil;
       
    40         UINavigationController *rightNavController = [[UINavigationController alloc] initWithRootViewController:rightController];
       
    41         [rightController release];
       
    42 
       
    43         // the contens on the left of the splitview, setting targetController that will receive push/pop actions
       
    44         SettingsBaseViewController *leftController = [[SettingsBaseViewController alloc] init];
       
    45         leftController.targetController = rightNavController.topViewController;
       
    46         UINavigationController *leftNavController = [[UINavigationController alloc] initWithRootViewController:leftController];
       
    47         [leftController release];
       
    48 
       
    49         self.splitViewRootController = [[MGSplitViewController alloc] init];
       
    50         self.splitViewRootController.delegate = nil;
       
    51         self.splitViewRootController.view.frame = screenRect;
       
    52         self.splitViewRootController.viewControllers = [NSArray arrayWithObjects: leftNavController, rightNavController, nil];
       
    53         self.splitViewRootController.showsMasterInPortrait = YES;
       
    54         [leftNavController release];
       
    55         [rightNavController release];
       
    56 
       
    57         // add view to main controller
       
    58         [self.view addSubview:self.splitViewRootController.view];
       
    59     } else {
       
    60         if (nil == self.baseController) {
       
    61             SettingsBaseViewController *sbvc = [[SettingsBaseViewController alloc] init];
       
    62             self.baseController = sbvc;
       
    63             [sbvc release];
       
    64         }
       
    65         self.baseController.targetController = nil;
       
    66         self.baseController.view.frame = screenRect;
       
    67 
       
    68         [self.view addSubview:self.baseController.view];
       
    69     }
       
    70 
       
    71     [super viewDidLoad];
       
    72 }
       
    73 
       
    74 #pragma mark -
       
    75 #pragma mark Memory management
       
    76 -(void) didReceiveMemoryWarning {
       
    77     if (self.baseController.view.superview == nil)
       
    78         self.baseController = nil;
       
    79     if (self.splitViewRootController.view.superview == nil)
       
    80         self.splitViewRootController = nil;
       
    81     MSG_MEMCLEAN();
       
    82     [super didReceiveMemoryWarning];
       
    83 }
       
    84 
       
    85 -(void) viewDidUnload {
       
    86     self.baseController = nil;
       
    87     self.splitViewRootController = nil;
       
    88     MSG_DIDUNLOAD();
       
    89     [super viewDidUnload];
       
    90 }
       
    91 
       
    92 -(void) dealloc {
       
    93     releaseAndNil(baseController);
       
    94     releaseAndNil(splitViewRootController);
       
    95     [super dealloc];
       
    96 }
       
    97 
       
    98 
       
    99 #pragma mark -
       
   100 #pragma mark view event management propagation
       
   101 // every time we add a uiviewcontroller programmatically we need to take care of propgating such messages
       
   102 // see http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/
       
   103 -(void) viewWillAppear:(BOOL)animated {
       
   104     [self.splitViewRootController.detailViewController viewWillAppear:animated];
       
   105     [self.baseController viewWillAppear:animated];
       
   106     [super viewWillAppear:animated];
       
   107 }
       
   108 
       
   109 -(void) viewWillDisappear:(BOOL)animated {
       
   110     [self.splitViewRootController.detailViewController viewWillDisappear:animated];
       
   111     [self.baseController viewWillDisappear:animated];
       
   112     [super viewWillDisappear:animated];
       
   113 }
       
   114 
       
   115 -(void) viewDidAppear:(BOOL)animated {
       
   116     [self.splitViewRootController.detailViewController viewDidAppear:animated];
       
   117     [self.baseController viewDidAppear:animated];
       
   118     [super viewDidAppear:animated];
       
   119 }
       
   120 
       
   121 -(void) viewDidDisappear:(BOOL)animated {
       
   122     [self.splitViewRootController.detailViewController viewDidDisappear:animated];
       
   123     [self.baseController viewDidDisappear:animated];
       
   124     [super viewDidDisappear:animated];
       
   125 }
       
   126 
       
   127 -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
       
   128     [self.splitViewRootController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
       
   129     [self.baseController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
       
   130 }
       
   131 
       
   132 -(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
       
   133     [self.splitViewRootController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
       
   134     [self.baseController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
       
   135 }
       
   136 
       
   137 -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
       
   138     [self.splitViewRootController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
       
   139     [self.baseController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
       
   140 }
       
   141 
       
   142 @end