当 UITabBarControllerDelegate 调用时 popToRootViewController 崩溃

发布于 2024-11-08 02:55:15 字数 1468 浏览 0 评论 0原文

我有一个带有 4 个 UINavigationControllers 的 UITabBarController。我已经实现了 didSelectViewController 委托方法,如下所示:

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

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

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

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

任何想法出了什么问题,如何解决,发生了什么?

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, what is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2024-11-15 02:55:15

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

在我的应用程序中,我在启动时有登录屏幕,然后我有带有 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];

希望这可以解决您的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文