gpt4 book ai didi

iphone - 当 UITabBarControllerDelegate 调用时 popToRootViewController 崩溃

转载 作者:行者123 更新时间:2023-12-03 20:16:06 27 4
gpt4 key购买 nike

我有一个带有 4 个 UINavigationControllersUITabBarController。我已经实现了 didSelectViewController 委托(delegate)方法,如下所示:

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

}
}

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

-(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 的调试器不会提供任何反馈。

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

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

它可以工作,但会泄漏内存。

有什么想法是错的,如何解决,发生了什么吗?

最佳答案

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

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

我已经在 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];

希望这能解决您的问题。

关于iphone - 当 UITabBarControllerDelegate 调用时 popToRootViewController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6066853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com