简体   繁体   中英

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 (ie, 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).

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];
}

There are a lot of questions in there and without seeing the entire program, it will be hard to answer them exactly, but Ill give some general advice.

On memory management, if you are using ARC (Automatic Reference Counting), you are not leaking anything. If you are not, you are leaking every time you alloc-init a UIView and add as a subview without then subsequently calling "release" on that view. When you add a UIView as a subview, the parent view retains it, therefore you as the caller can freely call release on it without worry of it being deallocated.

It seems like you want to do exactly what UINavigationController was designed for, presenting a series of views in a hierarchy. It allows you to push/pop to/from any view in the stack, and it keeps track of all the views for you. It really seems to me that it would be your best choice here.

Are you facing problem in this

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

that your current view is not removed or your next view will not display by using this code? If yes then you can use this

for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[BNG_AppViewController class]] || [view isKindOfClass:[NameListSelectionController class]] || [view isKindOfClass:[NameResultsController class]]) {
            [view removeFromSuperview];
        }
    } 

To remove views from superview and then add this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM