UINavigationController popToRootViewController,然后立即推送一个新视图 [英] UINavigationController popToRootViewController, and then immediately push a new view

查看:133
本文介绍了UINavigationController popToRootViewController,然后立即推送一个新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个选项卡的tabBarController,其中第一个包含一个NavigatorController实例。 navigatorController是使用自定义viewControllerpeersViewController启动的,它列出了tableView上的所有网络对等体。选择对等体后,FilesListViewController的实例(将c:\目录中的列表文件)推送到navigationController堆栈中。

I have a tabBarController with two tabs, first of which contains an instance of NavigatorController. The navigatorController is initiated with a custom viewController "peersViewController" that list all the network peers on a tableView. Upon selecting a peer, an instance of "FilesListViewController" (which list files in the c:\ directory) is pushed into the navigationController stack.

在这个filesListViewController中,我有一个按钮让它导航到说文档目录。为此,我将接口连接到rootViewController中调用gotoDirectory:(NSString *)路径方法:

In this filesListViewController I have a button to let it navigate to say documents directory. To do this I'd wired the interface to call a gotoDirectory:(NSString*)path method in the rootViewController:

- (void)gotoDirectory:(NSString*)path {
     [[self navigationController] popToRootViewControllerAnimated:YES];
     NSArray *files = [self getFilesFromPeerAtPath:path];
     FilesListViewController *filesVC = [[FilesListViewController alloc] initWithFiles:files];
     [[self navigationController] pushViewController:filesVC animated:YES];
     [filesVC release];
}

然而,当我按下那个按钮时,navigationController确实将我的视图弹出到了根视图控制器,但然后我没有出现我实例化的FilesListViewController。从日志中,我知道自定义的initWithFiles方法确实被调用,并且网络内容确实发生了获取文件名。

However, when I press that button, the navigationController did pop my view to the root view controller, but then the FilesListViewController that I instantiated did not appear. From the log, I know that the custom initWithFiles method was indeed called and network stuffs did happen to get the file names.

其他一些事情对此非常棘手。我尝试点击第二个标签,然后点击返回第一个标签,然后点击!我需要的文件名就在那里。看起来数据和filesListViewController确实被推入了navigatorController堆栈,但是显示没有刷新但是停留在rootViewController(peersViewController)的屏幕上。

Something else is screwy about this. I tried clicking on the second tab and then click back to the first tab, and huala! the file names I needed are there. It looks like the data and the filesListViewController was indeed pushed into the navigatorController stack, but the display was not refreshed but stuck at the screen of rootViewController (peersViewController).

我是做错了什么?

- 本。

- 在发布问题后编辑了15分钟。我找到了一个解决方法,但是让我困扰的是pop然后推不起作用。

-- Edited like 15 minutes after posting the question. I'd found a workaround, but it bothers me that pop and then push doesn't work.

- (void)gotoDirectory:(NSString*)path {
     PeersListViewController *rootViewController = (PeersListViewController*)[[[self navigationController] viewControllers] objectAtIndex:0];
     [[self navigationController] setViewControllers:[NSArray arrayWithObject:rootViewController]];
     FilesListViewController *filesVC = [[FilesListViewController alloc] initWithFiles:files];
     [[self navigationController] pushViewController:filesVC animated:YES];
     [filesVC release];
}

似乎应该以这种方式规避navigationController,而我可能必须释放原始堆栈中的所有viewControllers。但这确实适用于iphone 3.0模拟器。

It doesn't seem like the navigationController should be circumvented this way, and I'd probably have to release all the viewControllers that were in the original stack. This does however work on the iphone 3.0 simulator.

如果我正在使用此代码,应如何处理内存释放?我应该获得原始的NSArray视图控制器并释放所有内容吗?

If I'm using this code though, how should the memory release be handled? should I get the original NSArray of viewcontrollers and release everything?

推荐答案

这个问题和解决方案实际上非常简单。

The problem and solution to this issue is actually extremely simple.

调用 [self.navigationController popToRootViewControllerAnimated:YES] sets self.navigationController nil 。当您随后调用 [self.navigationController pushViewController:someOtherViewController] 时,您实际上是向 nil 发送消息,该消息不执行任何操作。

Calling [self.navigationController popToRootViewControllerAnimated:YES] sets self.navigationController to nil. When you subsequently call [self.navigationController pushViewController:someOtherViewController] you are effectively sending a message to nil, which does nothing.

要解决此问题,只需设置对navigationController的本地引用,然后使用它:

To workaround, simply set up a local reference to the navigationController and use that instead:

UINavigationController * navigationController = self.navigationController;
[navigationController popToRootViewControllerAnimated:NO];
[navigationController pushViewController:someOtherViewController animated:YES];

正如Jason所说,popToRootViewController必须在没有动画的情况下执行才能正常工作。

As stated by Jason, the popToRootViewController must be performed without animation for this to work correctly.

感谢在Apple论坛上 jpimbert

Thanks go to jpimbert on the Apple forums for pointing this out.

这篇关于UINavigationController popToRootViewController,然后立即推送一个新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆