cocoaTouch/SplitViewRootController.m
changeset 3305 91074496d5c9
child 3308 b6dcae4b6d2c
equal deleted inserted replaced
3304:8690a3aa93b5 3305:91074496d5c9
       
     1     //
       
     2 //  SplitViewRootController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 27/03/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "SplitViewRootController.h"
       
    10 #import "MasterViewController.h"
       
    11 #import "DetailViewController.h"
       
    12 
       
    13 @implementation SplitViewRootController
       
    14 @synthesize splitViewRootController, masterViewController, detailViewController;
       
    15 
       
    16 
       
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    18     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
       
    19 }
       
    20 
       
    21 -(void) didReceiveMemoryWarning {
       
    22     // Releases the view if it doesn't have a superview.
       
    23     [super didReceiveMemoryWarning];    
       
    24     // Release any cached data, images, etc that aren't in use.
       
    25 }
       
    26 
       
    27 // load the view programmatically; we need a splitViewController that handles a MasterViewController 
       
    28 // (which is just a UITableViewController) and a DetailViewController where we present options
       
    29 -(void) viewDidLoad {
       
    30     UINavigationController *detailedNavController;
       
    31     detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    32 
       
    33     Class splitViewController = NSClassFromString(@"UISplitViewController");
       
    34     if (splitViewController) {
       
    35         splitViewRootController = [[splitViewController alloc] init];
       
    36         CGRect screensize = [[UIScreen mainScreen] bounds];
       
    37         [[splitViewRootController view] setFrame:CGRectMake(0, 0, screensize.size.height, screensize.size.width)];
       
    38         masterViewController = [[MasterViewController alloc] initWithStyle:UITableViewStylePlain];
       
    39         
       
    40         UINavigationController *mainNavController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
       
    41         detailedNavController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
       
    42         
       
    43         // set attributes
       
    44         masterViewController.detailViewController = detailViewController;
       
    45         [splitViewRootController setViewControllers:[NSArray arrayWithObjects: mainNavController, detailedNavController, nil]];
       
    46         [mainNavController release];
       
    47         [detailedNavController release];
       
    48         
       
    49         [splitViewRootController setDelegate: detailViewController];
       
    50         
       
    51         // add view to main controller
       
    52         [self.view addSubview:[splitViewRootController view]];
       
    53         [detailViewController release];
       
    54         [masterViewController release];
       
    55     } else {
       
    56         detailedNavController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
       
    57         [self.view addSubview:detailedNavController.view];
       
    58         //[detailedNavController release];
       
    59     }
       
    60 
       
    61     [super viewDidLoad];
       
    62 }
       
    63 
       
    64 -(void) dealloc {
       
    65     [detailViewController release];
       
    66     [masterViewController release];
       
    67     [splitViewRootController release];
       
    68     [super dealloc];
       
    69 }
       
    70 
       
    71 #pragma mark -
       
    72 #pragma mark additional methods as we're using a UINavigationController programmatically
       
    73 // see http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/
       
    74 -(void) viewWillAppear:(BOOL)animated {
       
    75     [super viewWillAppear:animated];
       
    76     [detailViewController.navigationController viewWillAppear:animated];
       
    77 }
       
    78 
       
    79 -(void) viewWillDisappear:(BOOL)animated {
       
    80     [super viewWillDisappear:animated];
       
    81     [detailViewController.navigationController viewWillDisappear:animated];
       
    82 }
       
    83 
       
    84 -(void) viewDidAppear:(BOOL)animated {
       
    85     [super viewDidLoad];
       
    86     [detailViewController.navigationController viewDidAppear:animated];
       
    87 }
       
    88 
       
    89 -(void) viewDidDisappear:(BOOL)animated {
       
    90     [super viewDidUnload];
       
    91     [detailViewController.navigationController viewDidDisappear:animated];
       
    92 }
       
    93 
       
    94 
       
    95 @end