viewDidLoad(), LoadView()

发布于 2024-10-20 00:48:25 字数 137 浏览 2 评论 0原文

viewDidLoad()LoadView() 之间有什么区别?它们之间有何不同?

当我们在不使用 XIB 的情况下开发应用程序时,哪一个更好?

谢谢 。

What is the difference between viewDidLoad() and LoadView()? In what way are they different from each other?

Which one is better when we develop applications without using XIB ?

Thanks .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心的憧憬 2024-10-27 00:48:25

ViewDidLoad 在视图加载完成时调用,loadView 在加载开始时调用。

当您创建一个新项目时,您会看到对这些方法的注释,这些注释清楚地给出了您何时应该使用哪个函数的提示,

请参阅此

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

这些注释清晰且易于理解。

ViewDidLoad is called when your view loading is finished and loadView is called when loading starts.

And when you make a new project you see comments on these methods which clearly gives a tip when you should use which function

see this

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

These comments are clear and easy to understand.

对你再特殊 2024-10-27 00:48:25
viewDidLoad()

当您从 NIB 加载视图并希望在启动后执行任何自定义时使用。

LoadView()

当您想要以编程方式创建视图(不使用 Interface Builder)时,可以使用它。

viewDidLoad()

is to be used when you load your view from a NIB and want to perform any customization after launch.

LoadView()

is to be used when you want to create your view programmatically (without the use of Interface Builder).

天冷不及心凉 2024-10-27 00:48:25

如果您打算使用 IB 构建 UI,则应该在 viewDidLoad 中完成所有 IB 后初始化。如果您使用 nib 来初始化控制器,该类根本不会调用 loadView。

如果在代码中初始化控制器,viewController将首先调用loadView,然后调用viewDidLoad。您可以根据您的喜好在 loadView 或 viewDidLoad 中完成所有初始化。

但是,如果您决定使用 loadView,请务必在尝试读取 self.view 之前设置 view 属性,否则您将进入无限循环并崩溃。

If you intend to use IB to build your UI, you should do all your post IB initialization in viewDidLoad. The class will not call loadView at all if you use a nib to initialize a controller.

If you initialize the controller in code, the viewController will call loadView first, then viewDidLoad. You can do all your initialization in loadView, or viewDidLoad, depending on your preferences.

However, if you decide to use loadView, be sure to set the view property before attempting to read self.view, otherwise you will enter into an infinite loop and crash.

苍景流年 2024-10-27 00:48:25

如果您从 stroyboard 或 xib 文件初始化视图,请勿覆盖此方法或在内部调用 [super loadView]。
如果你在方法内部调用 [super loadView] ,你最好不要重写这个方法,并将以下代码放入你的 viewDidLoad 方法中。

如果您以编程方式初始化视图,则永远不应该调用[super loadView]。并且您必须将 rootView 分配给 self.view 属性,否则可能会发生完美崩溃。

If you initialize your view from stroyboard or xib file, don't override this method or call [super loadView] inside.
if you call [super loadView] inside the method, you better never override this method and put the following code to your viewDidLoad method.

if you initialize your view programmatically, you should NEVER call [super loadView]. and You must assign your rootView to self.view property, or you may get a perfect crash.

贱贱哒 2024-10-27 00:48:25

这不是很明显吗?

viewDidLoad 被调用...当视图完成加载时。

当视图被告知加载时,loadView 被调用。

两者都没有更好或更坏。这完全取决于您的设计。

祝你好运 :)

Isn't it obvious?

viewDidLoad is called... When the view finishes loading.

loadView is called when the view is told to load.

Neither is better or worse. It's all dependent on your design.

Good luck :)

往事随风而去 2024-10-27 00:48:25

如果没有关联的 nib,视图控制器会从与之关联的 nib 加载其视图,然后它会自动调用它的 loadView() 方法来填充它的视图。
在这种情况下,您需要实现 loadView() 方法。
默认情况下,

当您的视图加载到内存中时,它返回 nil viewDidLoad() 方法在这里被调用,您可以根据您的要求进行自定义初始化。

view controller loads its view from nib associated with it if there is no nib associated, then it automatically called it's loadView() method to fill it's View.
In that case you need to implement loadView() method.
by default it returns nil

when your view loads in to the memory viewDidLoad() method is called here you can do your custom initialization according to your requirement.

飘然心甜 2024-10-27 00:48:25

如果您正在开发应用程序而不使用xib,则调用LoadView()方法,并且如果有xib,则调用ViewDidLoad方法,

所以它更好使用LoadView方法。

If you are developing applications without using xib LoadView() method is called and if there is an xib then ViewDidLoad method is called

So it is better to use LoadView method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文