开发者

iPhone - Release viewcontroller after pop operation

When I close (pop) the viewcontroller how do I release it so that the next time it's run it will have to init again? And how do I init it again?

Let's say I have some cocos2开发者_如何学编程d game layer placed on that viewcontroller. I would like to add a button to close it (pop the view) and not run in background as it is now.

I'm simply using the code below to pop the current view but it doesn't "kill" it, just makes it disappear and go to root.

- (IBAction) closeThisOne {
    [self.navigationController popViewControllerAnimated:YES];
}

I create the view this way:

- (IBAction) buttonPressed {
if (self.mViewController == nil) {
    MainViewController *vc = [[MainViewController alloc]
                                      initWithNibName:@"MainView" bundle:[NSBundle mainBundle]];
    self.mViewController = vc;
    [vc release];
}
[self.navigationController pushViewController:self.mViewController animated:YES];
}

Simply.. how can I do something like: root view > start game view > back to root view (and release game) > start game view again

I'm still a bit confused how exactly navigation and cocos2d works.. Thanks in advance !

EDIT: OK! I see it now... The way I was pushing the viewcontroller I was retaining it at the same time. Just now I saw comparing to your posts, thanks.


After calling [self.navigationController pushViewController:vc animated:YES]; Its retain count will be incremented since it 'll be added to a stack. So after this line of code only you need to call [vc release];. And when you call [self.navigationController popViewControllerAnimated:YES]; it will be removed from the stack and its retain count is again decremented by one.


release the view controller after you have pushed it onto the UINavigationController stack. The UINavigationController will retain it.

 DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib" bundle:nil];
 // ...
 [self.navigationController pushViewController:detailViewController animated:YES];
 [detailViewController release];


When you are pushing the nextClass object at same time time just release that class object and that will release that viewController's object and navigationController will retain that object.

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib" bundle:nil]; // ... [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release];

and when you will pop that view object at that time navigation controller automatically release that retained object.


As far as I know [self.navigationController popViewControllerAnimated:YES]; will release the current viewcontroller that is present in the navigation stack. Then you have to allocate the view controller again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜