开发者

popToRootViewController crashes when called by UITabBarControllerDelegate

开发者 https://www.devze.com 2023-03-07 16:48 出处:网络
I have a UITabBarController with 4 UINavigationControllers.I have implemented the didSelectViewController Delegate Method as follows:

I have a UITabBarController with 4 UINavigationControllers. I have implemented the didSelectViewController Delegate Method as follows:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
    [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];

}
}

It crashes when a NavigationController is at a 2nd Level after didSelectRowAtIndexPath pushes a new viewController onto the stack.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// Navigation logic may go here. Create and push another view controller.

RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

// ...    
detailViewController.title = [self.temp objectAtIndex:indexPath.row];
detailViewController.sort = self.title;

// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

}

Of course the debugger with NSZombies enabled doesn't give any feedback.

However, if I add retain to detailViewController alloc;

RootViewController *detailViewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] retain];

It works, but leaks memory.

Any ideas what is wrong, how to fix开发者_JAVA技巧, what is happening?


I have the similar scenario and i came up with following solution.

In my application i have login screen at launch and then I have UITabbarController with 4 UINavigationControllers.

I have created property of UINavigationController in AppDelegate.h file.

@property (strong, nonatomic) UINavigationController *navigationController;

Then

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions         (NSDictionary *)launchOptions
{
 //Override point for customization after application launch.
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

return YES;
}

Now when you need to pop to RootViewController then use following code

#import "AppDelegate.h"

[((AppDelegate *)[[UIApplication sharedApplication] delegate]).navigationController popToRootViewControllerAnimated:YES];

Hope this solves your problem.

0

精彩评论

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