从Storyboard实例化View Controller与创建新实例 [英] Instantiate View Controller from Storyboard vs. Creating New Instance

查看:89
本文介绍了从Storyboard实例化View Controller与创建新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从故事板中实例化View Controller与创建它的新实例之间的功能区别是什么?例如:

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建议通过覆盖 loadView 方法执行此操作 UIViewController

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.

这篇关于从Storyboard实例化View Controller与创建新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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