Multiple Views

Discussion in 'iOS Programming' started by dant19, Jun 26, 2011.

  1. dant19 macrumors member

    Joined:
    May 28, 2008
    #1
    I am trying to update my application so that it has multiple views (it currently only has one).

    I have a UIViewController called "viewSwitcher" and then 3 other view controllers that manage their own views.

    In the app delegate I have made the "viewSwitcher" view a subview of the window, which works fine.

    Then in the viewDidLoad method of "viewSwitcher", I insert a new subview as follows:

    Code:
     
    MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
        menuViewController = menuController;
        
     
        
        [self.view addSubview:menuViewController.view];
    
    Again, this works fine.

    Then in the "menuViewController" I have the following method:


    Code:
    -(IBAction)viewNewGame{
    
        self.view.hidden = YES;
        [viewSwitcher switchNewGameView];
    }
    
    Which calls the following method from "viewSwitcher":

    Code:
    -(void)switchNewGameView{
        
      
        NSLog(@"switchNewGameView method starting");
        NewGameViewController *newController = [[NewGameViewController alloc] initWithNibName:@"NewGameViewController" bundle:nil];
        newGameViewController = newController;
        
        
        [self.view addSubview:newGameViewController.view];
    
        NSLog(@"switchNewGameView method finishing");
    
        
    //    NSString *alertText = [NSString alloc];
    //    alertText = [NSString stringWithFormat:@"You scored %d"];
    //    UIAlertView *alert = [[UIAlertView alloc]
    //                          initWithTitle:@"Game Over" 
    //                          message:alertText
    //                          delegate:self 
    //                          cancelButtonTitle:@"New Game" 
    //                          otherButtonTitles: nil];
    //    [alert show];
    }
    
    
    
    You'll notice that there are nslogs and commented out code for a UIAlert - this was to check the method was actually being called - which it is.

    But despite the method being executed, the newGameViewController view is not being inserted as a subview of "viewSwitcher"...

    Any thoughts? (Will clarify where required :) )

    Thanks.

    Dan
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    It sounds like you'd be better using a UINavigationController to allow you to push and pop view controllers. You can set it to hidden and not animate the transitions if you want. I say this as it handles all the issues of displaying and hiding the views for you.
     
  3. dant19 thread starter macrumors member

    Joined:
    May 28, 2008

Share This Page