UIViewController 指定初始化器与 loadView 方法

发布于 2025-01-02 18:05:40 字数 807 浏览 2 评论 0原文

我的视图控制器类 MyVC 从 UIViewController 类扩展。在指定的初始值设定项中,我将背景颜色更改为绿色,如下所示

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
    return self;
}

我还有 loadView 方法,该方法创建一个新的 UIView 对象并将其颜色更改为红色

- (void)loadView
{
    UIView* view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [view setBackgroundColor:[UIColor redColor]];
    [self setView:view];
    [view release];
}

在 loadView 调用之前调用指定的初始值设定项。所以我希望我的视图颜色(我在指定的初始化程序中设置为绿色)应该变成红色(我在 loadView 中设置)。 我看到我的颜色是绿色,如果我在指定的初始值设定项中评论绿色线,那么我会看到红色。那么,如果在初始化程序之后调用 loadView 方法,为什么它不覆盖 view 属性呢?

I have my view controller class MyVC extending from UIViewController class. In the designated initializer I change the background color to GREEN as following

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
    return self;
}

I also have the loadView method that creates a new UIView object and changes its color to RED

- (void)loadView
{
    UIView* view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [view setBackgroundColor:[UIColor redColor]];
    [self setView:view];
    [view release];
}

The designated initializer is called before loadView call. So I expect that my view color (which I set GREEN in designated initializer) should become RED (which I did in loadView).
I see my color GREEN and if I comment that GREEN color line in designated initializer, then I see the RED color. So why is it not overriding the view properties in loadView method if it is called after initializer?

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

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

发布评论

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

评论(2

审判长 2025-01-09 18:05:40

迦勒说得几乎是对的。当您访问视图控制器的 view 属性时,view 访问器方法会检查视图是否已加载。如果没有,它会调用 loadView,然后调用 viewDidLoad,然后返回视图。

初始化程序中的这一行访问 view 属性:

    [self.view setBackgroundColor:[UIColor greenColor]];

因此,要返回视图,view 访问器会调用您的 loadView 方法。您的 loadView 方法将视图的背景颜色设置为红色。然后你的初始化程序将背景颜色设置为绿色。

如果您在初始化程序和 loadView 方法中添加一些 NSLog,或者在 loadView 方法中放置断点,您将看到loadView 是从 view 调用的,而 view 又是从 initWithNibName:bundle: 调用的。

Caleb has it almost right. When you access a view controller's view property, the view accessor method checks whether the view has been loaded yet. If not, it calls loadView, then viewDidLoad, then returns the view.

This line in your initializer accesses the view property:

    [self.view setBackgroundColor:[UIColor greenColor]];

So to return the view, the view accessor calls your loadView method. Your loadView method sets the view's background color to red. Then your initializer sets the background color to green.

If you sprinkle some NSLogs in your initializer and your loadView method, or if you put a breakpoint in your loadView method, you will see that loadView is called from view, which is called from initWithNibName:bundle:.

硬不硬你别怂 2025-01-09 18:05:40

-loadView 的目的是,嗯,加载视图。当您访问视图控制器的 view 属性并且该属性的值为 nil 时,会调用它。在本例中,您将在初始化程序中访问 self.view,因此此时会调用 -loadView。您可以在此之后设置视图的背景,因此视图最终会呈现绿色背景。

The purpose of -loadView is to, uh, load the view. It's called when you access the view controller's view property and the value of that property is nil. In this case, you're accessing self.view in your initializer, so that's when -loadView gets called. You set the view's background after that happens, so the view ends up with a green background.

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