[英]How UINavigationController and UIViewController works?
I'm learning about UINavigationController
and UIViewController
s. 我正在学习
UINavigationController
和UIViewController
。 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
). 我的应用程序有一个导航控制器和两个视图控制器(我们称之为
FirstViewController
和SecondViewController
)。 When the app is launched navigation controller push the FirstViewController
on the stack. 当应用程序启动时,导航控制器将
FirstViewController
推FirstViewController
堆栈上。
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.