UIViewController 指定初始化器与 loadView 方法
我的视图控制器类 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
迦勒说得几乎是对的。当您访问视图控制器的
view
属性时,view
访问器方法会检查视图是否已加载。如果没有,它会调用loadView
,然后调用viewDidLoad
,然后返回视图。初始化程序中的这一行访问
view
属性:因此,要返回视图,
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, theview
accessor method checks whether the view has been loaded yet. If not, it callsloadView
, thenviewDidLoad
, then returns the view.This line in your initializer accesses the
view
property:So to return the view, the
view
accessor calls yourloadView
method. YourloadView
method sets the view's background color to red. Then your initializer sets the background color to green.If you sprinkle some
NSLog
s in your initializer and yourloadView
method, or if you put a breakpoint in yourloadView
method, you will see thatloadView
is called fromview
, which is called frominitWithNibName:bundle:
.-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'sview
property and the value of that property is nil. In this case, you're accessingself.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.