开发者

What's the difference between a ModalViewController and NonModalViewController in Iphone?

The difference of look between the two controllers.

And

Game *game = [[Game alloc] initWithNibName:@"mygame" bundle:nil];

I try to put a autorelease at the end of line. the program crashed.

Therefore I am wondering if

[self presentModalViewController:game animated:YES];

would increment reference count by 1?开发者_运维技巧

[self dismissModalViewControllerAnimated:YES];

would decrement the reference count by 1?


After presenting the controller just release it normally, make sure you don't send messages to game after releasing it though. presentModalViewController:animated: increases it's retain count so you are able to release it with out deallocating the object and you can successfully pass ownership to the current view controller.

Game *game = [[Game alloc] initWithNibName:@"mygame" bundle:nil];
[self presentModalViewController:game animated:YES];

[game release];

Then when it comes to dismissing it you shouldn't retain or release, just call the dismiss method.

Don't necessarily think about the retain count (and whatever you don't call the retainCount method on anything and decide what to write based on when is returned, that method is for legacy purposes only). Just match every init/new/copy with a release/autorelease.

(See listing 6-1 on this Apple doc, for prove that you should release it)


Effectively the [game release]; counteracts the init... and the dismissModalViewController... counteracts the presentModalViewController...


No, presenting a view controller only does that, it presents it. You should be calling retain and release at the appropriate times. It's important to keep track of your retain counts. Keep in mind that when you allocate and initialize, the retain count increases by one. You will have to release that object later.

If you're still unsure, you need to read the Apple docs on view controllers and memory management.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜