IOS。导航控制器。重用视图控制器
这是我的问题。 我有一个基于视图的应用程序。 我将 NavigationController
添加到 AppDelegate
中,如下所示。
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:_viewController];
navigationController = [nvc retain];
[nvc retain];
[self.window addSubview:navigationController.view];
这段代码是我从互联网上获得的。我不知道是否正确。
在所有视图控制器中,我正在执行以下操作来推送新视图。
UIViewController *newViewController = [[NewViewController alloc] init];
[self.navigationController pushViewController:newViewController animated:TRUE];
我不知道为什么
self.navigationController
在推送的视图中不返回 nil。它是否以某种方式使用 AppDelegate 导航控制器?如果我想重用一些视图控制器。我的意思是不要每次我想调用视图控制器时都分配 init 。我怎样才能做到这一点?在 AppDelegate 中存储引用?还有其他解决方案吗?
我的导航很复杂。即A、B、C是视图控制器。 A 调用 B。B 调用 C。C 调用 A。如果我继续调用,我每次都会创建一个新的视图控制器。如何重用这些视图控制器(或在调用新视图控制器时从堆栈中弹出并释放视图)?
谢谢。
Here is my problem.
I have a view based application.
I added a NavigationController
to the AppDelegate
as follows.
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:_viewController];
navigationController = [nvc retain];
[nvc retain];
[self.window addSubview:navigationController.view];
This code I got from internet. I don't know if it is correct.
In all the viewControllers I am doing the following to push a new view.
UIViewController *newViewController = [[NewViewController alloc] init];
[self.navigationController pushViewController:newViewController animated:TRUE];
I don't know why
self.navigationController
does not return nil in the pushed views. Is it using AppDelegate navigation controller somehow?If I want to reuse some view controllers. I mean not alloc init every time that I want to call the view controller. How can I do that? Store a reference in the
AppDelegate
? Any other solution?I have a complicated navigation. i.e. A, B, C are view controllers. A calls B. B calls C. C calls A. If I keep calling I am creating a new view controller each time. How can I reuse those view controllers (or pop and release the view from the stack when calling a new one)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)
navigationController
是UIViewController
的一个属性。如果视图控制器是导航控制器层次结构的一部分,那么它将被设置。2)你必须自己处理这个问题。在推送该视图控制器的对象中存储引用是实现此目的的常用方法。因此,如果 A 推送 B,则 A 会存储对 B 的引用,等等。
3) 无法让 UINavigationController 为您执行此操作。从你想要做的事情来看,我假设你来自 Android 背景? Android 有特定的方法来做到这一点,但在 iOS 中你必须自己编写代码。您可以通过查看 UINavigationControllerDelegate 及其 navigationController:didShowViewController:animated: 方法来完成此操作。您可以看到视图控制器何时被推送,以及它是否已经在堆栈中,然后更新导航堆栈(即
UINavigationController
的viewControllers
属性)以将其从原来的位置删除。但这可能会让用户感到非常困惑。1)
navigationController
is a property ofUIViewController
. If the view controller is part of a navigation controller hierarchy then it will be set.2) You'll have to manage this yourself. Storing a reference in the object that pushes that view controller would be the usual way to achieve this. So if A pushes B then A stores a reference to B, etc.
3) There's no way to get
UINavigationController
to do this for you. From what you're trying to do I assume you're coming from an Android background? Android has specific ways to do just that, but in iOS you'd have to code it yourself. You could do this by looking atUINavigationControllerDelegate
and itsnavigationController:didShowViewController:animated:
method. You could see when a view controller is pushed and if it's already in the stack then update the navigation stack (i.e. theviewControllers
property ofUINavigationController
) to remove it from where it was. But that might get quite confusing for the user.