开发者

What is wrong with this code? Trying to load a navController onto a modal view controller

I have a UIViewcontroller that I want to push onto a UINavigationController, which in turn would push onto a modal view using presentModalViewController:animated.

Here's my code:

TargetViewController *targetViewController = [[[TargetVie开发者_运维技巧wController alloc] init] autorelease];
UINavigationController *targetNavController = [[[UINavigationController alloc] initWithRootViewController:targetViewController] autorelease];
[self presentModalViewController:targetNavController animated:YES];

When code is run, the modal view loads as expected, but after dismissModalViewControllerAnimated: is called, the modal view slides down and the app crashes.

I get the following error in gdb: -[CALayer retain]: message sent to deallocated instance

First part of question: is there anything inherently wrong with the above code?

Second part: if there is nothing wrong with above code, where should I look next to debug?

Additional info: When I don't release or autorelease the navController, it works fine. But Instruments will show abandoned memory, which I can only assume is the navController not being released. Maybe the modal view controller

P.S. I know that the crash is related to the memory management of the above ViewController, navController and modal view, because my code was working prior to messing with this code.


You generally don't push a navController as a view as the navigation controller works as the root controller. The views are pushed from the navController. Once you have a view pushed, you could then present the next view modally.


What happens if you don't use autorelease?

I.E.:

TargetViewController *targetViewController = [[TargetViewController alloc] init];
UINavigationController *targetNavController = [[UINavigationController alloc] initWithRootViewController:targetViewController];
[targetViewController release];
[self presentModalViewController:targetNavController animated:YES];
[targetNavController release];


Fixed the issue. I was releasing the VC, causing the crash. Thanks for the input.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜