views:

580

answers:

1

Hi,

I want to remove the current subview (added atIndex:0) from my root view controller and insert a new view in its place.

Currently I can only find code snippets that show unloading a view using removeFromSuperView, which requires you to know what viewcontrollers view is currently loaded.

The only other thing to note is that I have another subview inserted in the rootcontrollers view which i dont want unloaded. So code that removes any and all subviews is not adequate

Thanks,

m

if(self.firstscr == nil)
    {
     firstscreen *f = [[firstscreen alloc] initWithNibName:@"firstscreenview" bundle:nil];
     self.firstscr = f;
     [f release];
    }

    ///This is my attempt at getting to the currently loaded view :P
  [[self.view subviews atIndex:0].view removeFromSuperView];


    [self.view insertSubview:firstscr.view atIndex:0];
+1  A: 

Here is how to do it (i figured it out, Yey me!)

if(self.firstscr == nil)
    {
     firstscreen *f = [[firstscreen alloc] initWithNibName:@"firstscreenview" bundle:nil];
     self.firstscr = f;
     [f release];
    }

    //Remove whatever view is currently loaded at index 0, this index is only to be used by "page" views
    UIView *v = [self.view.subviews objectAtIndex:0];
    [v removeFromSuperview];


    [self.view insertSubview:firstscr.view atIndex:0];
michael