使用loadView时无限循环

发布于 2024-10-14 20:49:34 字数 410 浏览 4 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(3

℡寂寞咖啡 2024-10-21 20:49:34

在这种情况下,进行无限循环的唯一方法是获取视图属性,直到其未设置为止。如果你写下一个(例如):

- (void)loadView {
   self.view = [[UIView alloc] initWithFrame:self.view.bounds];
}

你会得到无限循环,但

- (void)loadView {
   self.view = [[UIView alloc] initWithFrame:CGRectZero];
}

工作正常。

因此,在未设置视图属性之前,您无法访问它。

Only one way to make infinite loop in this case - is getting view property until its not set. If you write next (for example):

- (void)loadView {
   self.view = [[UIView alloc] initWithFrame:self.view.bounds];
}

You'll got infinite loop, but

- (void)loadView {
   self.view = [[UIView alloc] initWithFrame:CGRectZero];
}

works OK.

So you can't to access view property until you didn't set it.

叹梦 2024-10-21 20:49:34

由于您只是继承了超类(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.

夜吻♂芭芘 2024-10-21 20:49:34

如果您重写 loadView,则需要为控制器的视图层次结构提供根视图。如果不提供它,每次引用视图时都会调用 loadView,这可能会导致无限循环。来自文档:

如果手动指定视图,则必须实现 loadView 方法并使用它来将根视图对象分配给视图属性。

会导致无限循环的实现:

- (void)loadView {
    NSLog(@"loadview");
}

...loadView 之后 self.view 为 nil

- (void)loadView {
   self.view; // Or anything that references self.view
}

...引用 self.view 会导致调用 loadView,从而导致无限循环。

正确的:

- (void)loadView {
    self.view = [[UIView alloc] init];
    if (self.view == nil) {
        [super loadView]; // Try to load from NIB and fail properly, also avoiding inf. loop.
    }
}

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:

If you specify the views manually, you must implement the loadView method and use it to assign a root view object to the view property.

Implementations that would cause an infinite loop:

- (void)loadView {
    NSLog(@"loadview");
}

...self.view is nil after loadView

- (void)loadView {
   self.view; // Or anything that references self.view
}

...referencing self.view causes loadView to be invoked, hence an infinite loop.

Correct:

- (void)loadView {
    self.view = [[UIView alloc] init];
    if (self.view == nil) {
        [super loadView]; // Try to load from NIB and fail properly, also avoiding inf. loop.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文