views:

187

answers:

1

I am using Cocos2D for my main framework. In some cases, I want Cocos2D to load a nib file and have that be the view:

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TargetPlayerViewController *myController = [[TargetPlayerViewController alloc]initWithNibName:@"TargetPlayerViewController" bundle:nil];
[window addSubview:[myController view]];
[window makeKeyAndVisible];

This works as expected, and shows the TargetPlayerViewController. Wonderful!

What I need to know is: once that view has been loaded, how can I have the view remove itself? I've tried a few different ways, but all of them result in the program crashing.

To test I have a button on the view set up which triggers this method:

- (IBAction)GTFOnow:(id)sender {
    NSLog(@"GFTO");
    //window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //[self.view removeFromSuperview];
    //[window makeKeyAndVisible];
}

GTFOnow is a method in TargetPlayerViewController. When it is called, the current subview (that was called in the Cocos2D code above) should be removed from the window.

A: 

First of all, you shouldn't create a new window just because you want to remove a subview. Secondly, whatever else happens, this shouldn't cause the app to crash. In which class do you have the GTFOnowmethod? I suppose in the TargetPlayerViewController class?

Felixyz
The GTFOnow method is in the TargetPlayerViewController. I was hoping the view could remove itself from the window.
Wayfarer
Yes the view can be removed from the window (although it's the controller that is removing it, so it's not really removing "itself"). But you don't need to create a new window as you do in this code. Instead you need to get a reference to the window that was created at startup, in your app delegate. That's the window the view is a child of, and that's the one you want to remove it from. In general, you very rarely want to create a second window in an iPhone app.
Felixyz