从故事板实例化视图控制器与创建新实例 [英] Instantiate View Controller from Storyboard vs. Creating New Instance
问题描述
从故事板实例化视图控制器和创建它的新实例之间的功能区别是什么?例如:
What is the functional difference between instantiating a View Controller from the storyboard and creating a new instance of it? For example:
#import "SomeViewController.h"
...
SomeViewController *someViewController = [SomeViewController new];
对比
#import "SomeViewController.h"
...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SomeViewController *someViewController = [storyboard instantiateViewControllerWithIdentifier:@"SomeViewController"];
在这两种情况下,someViewController
是否实际上是一回事?
In either case, is someViewController
effectively the same thing?
推荐答案
主要区别在于 UIViewController
的子视图如何被实例化.
The main difference is in how the subviews of your UIViewController
get instantiated.
在第二种情况下,您在故事板中创建的所有视图都将自动为您实例化,并且所有出口和动作都将按照您在故事板中的指定进行设置.
In the second case, all the views you create in your storyboard will be automatically instantiated for you, and all the outlets and actions will be set up as you specified in the storyboard.
在第一种情况下,这些都不会发生;你只是得到原始对象.您需要分配和实例化所有子视图,使用约束或其他方式布置它们,并自己连接所有出口和动作.Apple 建议通过覆盖UIViewController<的
loadView
方法来实现这一点/代码>.
In the first, case, none of that happens; you just get the raw object. You'll need to allocate and instantiate all your subviews, lay them out using constraints or otherwise, and hook up all the outlets and actions yourself. Apple recommends doing this by overriding the loadView
method of UIViewController
.
这篇关于从故事板实例化视图控制器与创建新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!