gpt4 book ai didi

ios - popToRootViewControllerAnimated 使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 12:37:01 29 4
gpt4 key购买 nike

我的应用程序包含一些 UIViewControllers。第一个是登录页面,第二个是“菜单页面”,第三个是 viewController

我在每个页面的按钮上放置了一个注销功能,带有如下所示的 alertView:

- (IBAction)logout:(id)sender {
UIAlertView *logoutConfirm = [[UIAlertView alloc] initWithTitle: @"Logout" message: @"Sure ? " delegate: self cancelButtonTitle: @"Ok" otherButtonTitles:@"Cancel", nil];

[logoutConfirm show];
}

// Logout
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Logs to track the bug
NSLog(@"LOG :%@", [self.navigationController viewControllers]);
NSLog(@" LOG 2 : %@", [self.navigationController.viewControllers objectAtIndex:0]);

if (buttonIndex == 0) {
[self.navigationController popToRootViewControllerAnimated:NO];
}
}

popToRootViewControllerAnimated 使应用程序在第三个 ViewController 上崩溃,但在第二个上没有此消息:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller <MainViewController: 0xXXXXXX> as a child of view controller:<UINavigationController: 0xXXXXXX>'

有我的日志:

2014-09-25 10:24:35.074 Poject[18594:654203] LOG :(
"<MainViewController: 0xXXXXXXX>",
"<secondViewController: 0xXXXXXXX>",
"<thirdViewController: 0xXXXXXXX>"
)
2014-09-25 10:24:35.075 Poject[18594:654203] LOG 2 : <MainViewController: 0xXXXXXXX>

似乎我正在添加一个 UIViewController,但只是想回到第一个 viewController ...我认为如果它在第二个 ViewController 上工作而不是在第三个 ViewController 上工作很奇怪 ...

更新

@mehul patel 让我在我的 AppDelegate.h 中评论这一行:

[self.window addSubview:self.navigationController.view];

因为:

You can not add your rootViewController as subview to window, it will crash in any way

但是现在,我的 firstViewController 中“登录按钮”上的 pushViewController 不再起作用了。它什么都不做,所以我 NSLog 我的 navigationController 的 Controller 数组

我第一次得到:

2014-09-25 16:48:07.512 Project[22023:780717] (
"<MainViewController: 0xf81f290>"
)

我第二次得到:

2014-09-25 16:48:12.104 Project[22023:780717] (
"<MainViewController: 0xf81f290>",
"<MenuSelectionViewController: 0xd929030>"
)

但是, View 并没有改变......

感谢您的帮助!

最佳答案

试试这个

- (void) logout {

@try {
NSArray *controllers = [self.navigationController viewControllers];

if (controllers && [controllers count] > 0) {
UIViewController *loginController = [controllers objectAtIndex:0];
[self.navigationController popToViewController:loginController animated:YES];
}
}
@catch (NSException *exception) {
// Throws an exception
}
}

关于ios - popToRootViewControllerAnimated 使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034055/

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