开发者

iPhone - Loading a new view and a deallocated object

开发者 https://www.devze.com 2023-02-05 15:17 出处:网络
So I feel like a serious rookie right now, but I have a problem I can\'t seem to figure out.I have a barebones app, with literally nothing in it except a login screen and a second view containing a ta

So I feel like a serious rookie right now, but I have a problem I can't seem to figure out. I have a barebones app, with literally nothing in it except a login screen and a second view containing a tableview. When I add the second view after logging in (I have done this like 4 times before...), the table view goes through its delegates and appears that it's going to load, but something happens. I have enabled my NSZombies, and it appears to be deallocating the new开发者_JAVA技巧 view, right before it appears.

After tracing through it, and building up again piece by piece, it appears to happen after I wire the table to the view as the datasource/delegate in IB. I have set the view as a UITableViewDelegate, and the methods indeed get fired. Does anyone have any idea what might be causing this behavior?


Have you added the 'second'view to an exisitng view using addSubview: or added it to some form of UINavigationController or UITabBarController? When you do this it will automatically increase the retain count and whatever code you have releasing the view won't cause is to be deallocated.

In my AppDelegate application:didFinishLaunchingWithOptions I have something like;

LoginViewController *login = [[LoginViewController alloc] init];    
[login setDelegate:self];

loginNavController = [[UINavigationController alloc]      
                       initWithRootViewController:login];

[window addSubview:[loginNavController view]];

And then once login has occured (and succeeded using a protocol/delegate to send the message back to AppDelegate) I call this code;

UIViewController *newView1 = [[UIViewController alloc] init];
UIViewController *newView2 = [[UIViewController alloc] init];
UIViewController *newView3 = [[UIViewController alloc] init];

myTabBarController = [[UITabBarController alloc] init];

myNavController = [[UINavigationController alloc] 
                     initWithRootViewController:newView1];

// nav controller now retaining
[newView1 release];

NSArray *viewControllers = [NSArray arrayWithObjects:myNavController, 
                                                     newView2, 
                                                     newView3, 
                                                     nil];

[myTabBarController setViewControllers:viewControllers animated:YES];   
[[myTabBarController view] setFrame:[[UIScreen mainScreen] applicationFrame]];

[window addSubview:[tabBarController view]];    

// tab bar controller now retaining
[newView2 release];
[newView3 release];

// remove login from application
[[loginNavController view] removeFromSuperview];

The AppDelegate has the following declared in the header file;

LoginViewController *loginViewController;
UITabBarController *myTabBarController;
UINavigationController *myNavController;

In the dealloc method for the AppDelegate these are released.

This gives me my login page and then when that has processed my views with a top nav all controlled using the bottom tab bar.

Hope this helps in some way.


You have either too many release (or autorelease) calls - or not enough retain calls - in your view loading/transitioning code, but it's impossible to be more specific without seeing that code.

What's probably happening is the autorelease pool is being flushed between your view loading and your view being shown, and that's what's leading the behaviour you describe.

0

精彩评论

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