Search code examples
iphoneuiviewuiwindow

iPhone: force from subview to remove itself


In appdidfinishlaunch I'm loading a tabbarcontroller as subview and after that im loading an other view

MySubView * mySubView = [[MySubView alloc] init];
[window addSubview:mySubView];
[mySubView release];

I want to close that toplayer with a buttonclick in the subview, so I set up an IBAction and tried diffrent things to force the actual view to close:

// 1.
[self.view removeFromSuperview];

// 2.
id  *delegate = [[UIApplication sharedApplication] delegate];
[[[delegate view] objectAtIndex:0] removeFromSuperview];


//3.
[[[delegate window] view] removeFromSuperview];

So how can i pop this subview from window ?

cheers Simon


Solution

  • You could do a couple of things. One way would be to assign a unique tag to the view and get it using that tag later, so:

    MySubView* mySubView = [[MySubView alloc] init];
    [mySubView setTag:100];
    [window addSubview:mySubView];
    [mySubView release];
    
    // later
    
    [[[delegate window] viewWithTag:100] removeFromSuperview];
    

    Another is to iterate through the window's subviews until you find one that is an instance of your unique class, then remove that. So:

    MySubView* mySubView = nil;
    for( UIView* view in [[delegate window] subviews] ) {
      if( [view isKindOfClass:[MySubView class]] ) {
        mySubView = (MySubView*)view;
        break;
      }
    }
    [mySubView removeFromSuperview];