使用loadView时无限循环
在 UIViewController 中使用 loadView 时出现非常有趣的问题。
通常我们这样使用
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSLog(@"loadview");
[super loadView];
}
如果删除
[super loadView];
我们会因此陷入死循环
- (void)loadView {
NSLog(@"loadview");
}
为什么?
Very interesting problem when using loadView in UIViewController.
Usually we used like this way
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSLog(@"loadview");
[super loadView];
}
If remove
[super loadView];
We will get dead loop with this
- (void)loadView {
NSLog(@"loadview");
}
Why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,进行无限循环的唯一方法是获取视图属性,直到其未设置为止。如果你写下一个(例如):
你会得到无限循环,但
工作正常。
因此,在未设置视图属性之前,您无法访问它。
Only one way to make infinite loop in this case - is getting view property until its not set. If you write next (for example):
You'll got infinite loop, but
works OK.
So you can't to access view property until you didn't set it.
由于您只是继承了超类(UIViewController)中正在实现的内容,因此如果您不调用超级方法,那么需要完成的实现就不会完成。
几乎所有 super 方法都会做一些关键的事情,并且继承超类实现的本地类必须一起重写它们(除非您通过参考文档了解 super 的所有功能,否则这永远不是一个好主意),或者只是将本地类实现添加到继承的超类实现。
总之,每当您继承一个类(在软件开发的大多数情况下)时,您都应该让超类执行其实现,除非可以安全地覆盖它们。
如果我是正确的,似乎 super loadView 实现了一些非常关键的东西来避免循环。
附加说明:
但是,根据文档,您不应该调用超级方法:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
无限循环的原因可能是没有正确实现
view
属性造成的。Since you are just INHERITING what's being implemented in super class(UIViewController), if you don't call super methods, then implementation that needs to be done is NOT done.
Almost all super methods do something critical and the local class inheriting super class implementations must either override them all together (unless you know everything about what super does by referring to the documentation, it's never a good idea), or just ADD local class implementation to the inherited super class implementations.
In conclusion, whenever you inherit a class, which is in most cases of software development, you should let the super class do its implementations unless it's safe to override them.
If I am correct, it seems like super loadView implements something very critical to avoid the loop.
ADDITIONAL NOTE:
However, based on the documentation, you should not call super method: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
Probably, the reason for infinite loop is caused by not implementing
view
property appropriately.如果您重写 loadView,则需要为控制器的视图层次结构提供根视图。如果不提供它,每次引用视图时都会调用 loadView,这可能会导致无限循环。来自文档:
会导致无限循环的实现:
...loadView 之后 self.view 为 nil
...引用 self.view 会导致调用 loadView,从而导致无限循环。
正确的:
If you override loadView, you are expected to provide a root view for the controller's view hierarchy. If you don't provide it loadView would get called every time the view is referenced, possibly leading to an infinite loop. From the docs:
Implementations that would cause an infinite loop:
...self.view is nil after loadView
...referencing self.view causes loadView to be invoked, hence an infinite loop.
Correct: