Search code examples
iphoneobjective-ciosxcodesubviews

Adding and removing subviews in series


I am having difficulty managing the views in my app, and would appreciate anyone providing some clarification. In my mind, the program is structured as a hierarchy in the following manner.

    BNG_AppViewController:UIViewController  

-calls-

    NameListSelectionController:UIViewContoller

-calls-

    GameViewController:UIViewController

-calls-

    NameResultsController:UIViewController

All of the subviews, which are built using XIBs from the interface builer are called in a manner identical to the following:

    NameListSelectionController *NameListSelectionControllerScreen = [[NameListSelectionController alloc] initWithNibName:@"NameListSelectionController" 
              bundle:nil];
    [self.view addSubview:NameListSelectionControllerScreen.view];

When I want to move up my view hierarchy, I use a call identical to the following from within the controller class -- usually using a button to trigger the call.

    [self.view removeFromSuperview];

Using this method, I can move up and down my hierachy in a linear fashion.

However, what I would like to be able to do is, when I am moving back up my heirarchy, skip the 2nd level controller (i.e., NameListSelectionController above) and go directly from the 3rd level to the 1st level.

Thus far, I've tried removing the 2nd level view from the hierarchy when calling my third level on my way down the hierarchy, to no avail.

    [self.view removeFromSuperview];
    gameScreen = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil];
    [self.view addSubview:gameScreen.view];

However, I seem to end back up at my 1st level, without any obvious effect to the second and third lines of the code. I've also tried sending the 2nd level of the hierarchy to the back, without any results.

I'd appreciate any pointers in the right direction, including any big picture ideas for how to revise the structure of my program. I need to direct the user in the linear fashion described above, but I don't know that I need the program to be built that way.

I did read all the documentation on subviews and the removeFromSuperview call I could find, but did not see a method for doing what I wanted or I did not understand what I read. I did look into the possibility of using a NavigationController, but that didn't seem to offer any advantages given what I was trying to do (or maybe I couldn't see them).


Solution

  • for that purpose you can use navigation controller.
    and when you want to skip one pop up, you can use -[UINavigationController popToRootViewControllerAnimated:] or -[UINavigationController popToViewController:animated:] to accomplish this.

    -(void)goToMainCategoryView;
    {
        id object = nil;
    
        for (UIViewController *viewControl in self.navigationController.viewControllers)
        {
            if(viewControl.view.tag == 0)
            {
                object = viewControl;
            }
        }
        [self.navigationController popToViewController:object animated:YES];
    }