Multiple Views

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

  1. dant19 macrumors member

    May 28, 2008
    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:

    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:

        self.view.hidden = YES;
        [viewSwitcher switchNewGameView];
    Which calls the following method from "viewSwitcher":

        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 :) )


  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    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

    May 28, 2008

Share This Page