简体   繁体   English

UINavigationController和UIViewController如何工作?

[英]How UINavigationController and UIViewController works?

I'm learning about UINavigationController and UIViewController s. 我正在学习UINavigationControllerUIViewController Here is the simple app I build. 这是我构建的简单应用程序。 Notice that I use ARC. 请注意,我使用ARC。

My app have a navigation controller and two view controllers (let's call them FirstViewController and SecondViewController ). 我的应用程序有一个导航控制器和两个视图控制器(我们称之为FirstViewControllerSecondViewController )。 When the app is launched navigation controller push the FirstViewController on the stack. 当应用程序启动时,导航控制器将FirstViewControllerFirstViewController堆栈上。

In FirstViewController I have a button which push the SecondViewController when is touched. FirstViewController我有一个按钮,可以在触摸时按下SecondViewController。 Here is some code. 这是一些代码。

FirstViewController.m FirstViewController.m

-(IBAction)pushSecondViewController
{
    SecondViewController *secondViewController = [SecondViewController alloc]init];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

In the second view controller I have a button which pop the current view controller from the stack. 在第二个视图控制器中,我有一个按钮,从堆栈中弹出当前视图控制器。

SecondViewController.m SecondViewController.m

-(IBAction)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

So far, so good. 到现在为止还挺好。 Here are my questions: 这是我的问题:

Does the navigationController check for an existing instance of SecondNavigationController and if such not exist then it creates a new one? navigationController是否检查SecondNavigationController的现有实例,如果不存在则会创建一个新实例?

If not, should I use singleton to make sure that only one instance is created and reused instead of creating a new instance every time when the button which push the SeconViewController is touched? 如果没有,我应该使用单例来确保每次触摸推动SeconViewController的按钮时,只创建并重用一个实例而不是创建一个新实例吗?

With your current code, the second view controller will be destroyed when it is popped from the stack, so no, the navigation controller won't re-use it. 使用当前代码,第二个视图控制器在从堆栈弹出时将被销毁,因此,导航控制器不会重复使用它。

If you really want to keep the second view controller around, make it a strong property of the first view controller, but don't do this unless you actually have a reason to - the method you are using is standard and creating a new view controller is generally preferred to taking up lots of memory with view controllers that aren't even on screen. 如果你真的想要保留第二个视图控制器,使它成为第一个视图控制器的强大属性,但除非你实际有理由,否则不要这样做 - 你正在使用的方法是标准的并且创建一个新的视图控制器通常首选占用大量内存,而视图控制器甚至不在屏幕上。 Memory is more scarce than processor resource, creating view controllers happens all the time. 内存比处理器资源更稀缺,创建视图控制器一直在发生。

I agree with jrturton and I add the following guidelines. 我同意jrturton,并添加以下指南。

First, in my opinion it's not a good idea to make controllers as singletons. 首先,在我看来,将控制器作为单身人士并不是一个好主意。

Then, you have to check yourself if an instance of some type exists in the UINavigationController "controllers array". 然后,你必须检查一下UINavigationController “controllers array”中是否存在某种类型的实例。

@property(nonatomic, copy) NSArray *viewControllers

Finally, you could create a strong reference for your controller but it's not necessary at all. 最后,您可以为您的控制器创建一个强大的参考,但它根本没有必要。 The creation of a new controller it's very quickly. 它很快就能创建一个新的控制器。 Instead of having a strong reference of it, I would cache data presented on it, if any. 如果有的话,我会缓存上面显示的数据,而不是强烈引用它。 This to avoid the user to wait for already downloaded data. 这样可以避免用户等待已下载的数据。

Hope it helps. 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在uinavigationcontroller堆栈中检查uiviewcontroller - How to check a uiviewcontroller is present in uinavigationcontroller stack 如何以编程方式将UINavigationController添加到现有的UIViewController - How to add an UINavigationController to an existing UIViewController programmatically 如何使用Interface Builder创建带有UINavigationController的UIViewController? - How to create an UIViewController with UINavigationController with Interface Builder? 如何根据 UINavigationController 中显示的 UIViewController 更改 UINavigationBar - How to change UINavigationBar depending on the UIViewController shown in UINavigationController 如何深层链接到从UITabBarViewController中的UINavigationController中的UIViewController访问的UIViewController? - How to deeplink to a UIViewController accessed from a UIViewController in a UINavigationController in a UITabBarViewController? 如何在不导入第一个UIViewController类的情况下从UINavigationController中的另一个UIViewController手动取消分配UIViewController? - How can I dealloc manually UIViewController from another UIViewController in UINavigationController without import the first UIViewController class? 将UIViewController转换(或包装)到UINavigationController中 - Trasform ( or Wrap ) UIViewController into UINavigationController 替换 UINavigationController 层次结构中的 UIViewController - Replace a UIViewController in the UINavigationController hierarchy UINavigationController到UIViewController的困难 - UINavigationController to UIViewController difficulty UIViewController在uinavigationcontroller堆栈中不可见 - UIViewcontroller not visible in the uinavigationcontroller stack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM