当UITabBarControllerDelegate调用时,popToRootViewController崩溃

时间:2011-05-20 02:24:04

标签: iphone uinavigationcontroller

我有UITabBarControllerUINavigationControllers。我已经实现了didSelectViewController委托方法,如下所示:

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

}
}

NavigationController将新的viewController推入堆栈后,当didSelectRowAtIndexPath处于第二级时,它会崩溃。

-(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];

}

当然启用了NSZombies的调试器不会给出任何反馈。

但是,如果我向detailViewController alloc添加retain;

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

它有效但泄漏记忆。

任何想法有什么不对,如何解决,发生了什么?

1 个答案:

答案 0 :(得分:0)

我有类似的情况,我提出了以下解决方案。

在我的应用程序中,我在启动时有登录屏幕,然后我有UITabbarController和4个UINavigationControllers。

我在AppDelegate.h文件中创建了UINavigationController的属性。

@property (strong, nonatomic) UINavigationController *navigationController;

然后

- (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;
}

现在,当您需要弹出RootViewController时,请使用以下代码

#import "AppDelegate.h"

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

希望这能解决你的问题。