按下后退按钮时 UINavigationController 不会弹出视图,但导航栏会动画

发布于 2024-12-17 06:17:59 字数 2146 浏览 0 评论 0原文

我正在创建一个非常简单的示例应用程序,以展示首选项如何与我正在开发的新应用程序配合使用。过程非常简单:在IB中创建一个UINavigationController,为其分配将成为根视图控制器的视图控制器,并在调用didSelectRowAtIndexPath时将新的视图控制器推送到导航控制器的堆栈中。

我已经阅读了尽可能多的相关主题,但从未找到令人满意的答案。

问题是,当按下后退按钮时,导航控制器会动画返回,但视图本身不会变回原样,这意味着它不会弹出。

这是我用来将新视图控制器推入堆栈的代码。这是相当标准的。我添加了 NSLog 以确保它只被命中一次。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section != 0)
    return;

NSLog(@"didSelectRowAtIndexPath\n");

PrefsListTableViewController *prefsListTableViewController = [[PrefsListTableViewController alloc] initWithNibName:@"PrefsListTableViewController" bundle:nil];

[self.navigationController pushViewController:prefsListTableViewController animated:YES];
[prefsListTableViewController release];
}

为了检查 viewWillDisappear 调用是否在第二个视图上被命中,我将其添加到我的 PrefsListTableViewController 中:

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"ptvc viewWillDisappear");
}

这永远不会被命中。

首先,我的印象是您不需要编写任何代码来处理后退按钮,因为当您按下后退按钮时 UINavigationController 应该自动为您弹出堆栈。我可以通过一个示例应用程序来展示我正在做的事情,但示例只是代码。该代码来自 Erica Sadun 的 iPhone Developer's Cookbook 代码,配方 11-11。她的代码在这里:

https://github.com/ erica/iphone-3.0-cookbook-/tree/master/C11-Tables/11-Disclosure%20Chevrons

如果你查看她的代码,就会发现没有 .xib 文件,而一切都一样根本无需编写后退按钮代码即可处理。视图控制器是用代码推送的,然后弹出的,看起来不需要代码。我更喜欢使用 .xib 文件,一切都应该以相同的方式工作,但事实并非如此。

其次,我放入 NSLog 语句来表明实际上根导航控制器有两个视图控制器。

2011-11-18 11:14:36.355 TableViewTest[58011:207] didSelectRowAtIndexPath
2011-11-18 11:14:36.358 TableViewTest[58011:207] ptvc viewWillAppear (
"<TableViewController: 0x8923d40>",
"<PrefsListTableViewController: 0x8927a20>"
)
2011-11-18 11:14:36.717 TableViewTest[58011:207] ptvc viewDidAppear (
"<TableViewController: 0x8923d40>",
"<PrefsListTableViewController: 0x8927a20>"
)

因此您可以看到视图控制器已按正确的顺序推送。

这就引出了一个问题:为什么按下后退按钮时 PrefsListTableViewController 没有弹出?如果有人需要更多信息,请告诉我。

I'm creating a very simple sample app to show how preferences will work with a new application I'm working on. The process is very simple: create a UINavigationController in IB, assign it the View Controller that will be the root view controller, and push a new view controller onto the navigation controller's stack when didSelectRowAtIndexPath is called.

I've read as many related topics to this as I can find and never found a satisfactory answer.

The problem is that when the back button is pressed, the navigation controller animates back, but the view itself doesn't change back, meaning it's not getting popped.

This is the code I use to push the new view controller onto the stack. It's pretty standard. I added the NSLog to make sure it's only getting hit once.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section != 0)
    return;

NSLog(@"didSelectRowAtIndexPath\n");

PrefsListTableViewController *prefsListTableViewController = [[PrefsListTableViewController alloc] initWithNibName:@"PrefsListTableViewController" bundle:nil];

[self.navigationController pushViewController:prefsListTableViewController animated:YES];
[prefsListTableViewController release];
}

To check if the viewWillDisappear call is hit on the second view, I added this to my PrefsListTableViewController:

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"ptvc viewWillDisappear");
}

This never gets hit.

First, I was under the impression that you don't need to code anything to handle the back button since the UINavigationController should pop the stack for you automatically when you press the back button. I can show that with a sample app I have which does what I'm doing, but the sample is code only. The code is from Erica Sadun's iPhone Developer's Cookbook code, recipe 11-11. Her code is here:

https://github.com/erica/iphone-3.0-cookbook-/tree/master/C11-Tables/11-Disclosure%20Chevrons

If you look at her code, there are no .xib files, and everything is handled without the need to code the back button at all. The view controllers are pushed in code, and popped, seemingly, without code. I prefer to use .xib files and everything SHOULD be working the same way, but it's not.

Second, I put in NSLog statements to show that in fact, the root navigation controller has the two view controllers.

2011-11-18 11:14:36.355 TableViewTest[58011:207] didSelectRowAtIndexPath
2011-11-18 11:14:36.358 TableViewTest[58011:207] ptvc viewWillAppear (
"<TableViewController: 0x8923d40>",
"<PrefsListTableViewController: 0x8927a20>"
)
2011-11-18 11:14:36.717 TableViewTest[58011:207] ptvc viewDidAppear (
"<TableViewController: 0x8923d40>",
"<PrefsListTableViewController: 0x8927a20>"
)

So you can see that the view controllers are pushed and in the proper order.

That leads to one question: why isn't PrefsListTableViewController getting popped when the back button is pressed? If anyone needs any more info, please let me know.

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

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

发布评论

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

评论(1

锦欢 2024-12-24 06:17:59

你扩展了 UINavigationController 类吗?我做了并且遇到了同样的问题。显然,根据苹果文档,你不应该扩展该类......

Have you extended UINavigationController Class? I did and had the same issue. Apparently, according to apple docs, your not supposed to extend that class...

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