ios iPhone SDK:loadView 和 viewDidLoad 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/573958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone SDK: what is the difference between loadView and viewDidLoad?
提问by ryan.scott
When working with views and view controllers in an iPhone app, can anyone explain the difference between loadView and viewDidLoad?
在 iPhone 应用程序中使用视图和视图控制器时,谁能解释 loadView 和 viewDidLoad 之间的区别?
My personal context, is that I build all my views from code, I do not and will not use Interface Builder, should that make any difference.
我的个人情况是,我从代码构建我的所有视图,我不会也不会使用 Interface Builder,如果这有什么不同的话。
I've found that often when I add init code to loadView, I end up with an infinite stack trace, so I typically do all my child-view building in viewDidLoad...but it's really unclear to me when each gets executed, and what is the more appropriate place to put init code. What would be perfect, is a simple diagram of the initialization calls.
我发现通常当我向 loadView 添加初始化代码时,我最终会得到一个无限的堆栈跟踪,所以我通常在 viewDidLoad 中构建我所有的子视图......但是我真的不清楚每个何时被执行,并且放置初始化代码更合适的地方是什么。最完美的是初始化调用的简单图表。
Thanks!
谢谢!
回答by Marco
I can guess what might be the problem here, because I've done it:
我可以猜到这里可能有什么问题,因为我已经做到了:
I've found that often when I add init code to loadView, I end up with an infinite stack trace
我发现通常当我向 loadView 添加初始化代码时,我最终会得到一个无限的堆栈跟踪
Don't read self.view in -loadView.Only setit, don't getit.
不要在-loadView 中读取self.view。只设置它,不要得到它。
The self.view property accessor calls-loadView if the view isn't currently loaded. There's your infinite recursion.
如果当前未加载视图,则 self.view属性访问器调用-loadView。这是你的无限递归。
The usual way to build the view programmatically in -loadView, as demonstrated in Apple's pre-Interface-Builder examples, is more like this:
在 -loadView 中以编程方式构建视图的常用方法,如 Apple 的 pre-Interface-Builder 示例中所示,更像是这样:
UIView *view = [[UIView alloc] init...];
...
[view addSubview:whatever];
[view addSubview:whatever2];
...
self.view = view;
[view release];
And I don't blame you for not using IB. I've stuck with this method for all of Instapaper and find myself much more comfortable with it than dealing with IB's complexities, interface quirks, and unexpected behind-the-scenes behavior.
我不怪你不使用IB。我对 Instapaper 的所有内容都坚持使用这种方法,并且发现自己对它比处理 IB 的复杂性、界面怪癖和意想不到的幕后行为要舒服得多。
回答by NilObject
loadView
is the method in UIViewController
that will actually load up the view and assign it to the view
property. This is also the location that a subclass of UIViewController
would override if you wanted to programatically set up the view
property.
loadView
是UIViewController
实际加载视图并将其分配给view
属性的方法。UIViewController
如果您想以编程方式设置view
属性,这也是 的子类将覆盖的位置。
viewDidLoad
is the method that is called once the view has been loaded. This is called after loadView is called. It is a place where you can override and insert code that does further initial setup of the view once it has been loaded.
viewDidLoad
是在视图加载后调用的方法。这是在调用 loadView 之后调用的。在此位置,您可以覆盖和插入代码,以便在加载视图后对视图进行进一步的初始设置。
回答by ashokdy
viewDidLoad()
is to be used when you load your view from a NIB and want to perform any customization after launch
当您从 NIB 加载视图并希望在启动后执行任何自定义时使用
LoadView()
is to be used when you want to create your view programmatically (without the use of Interface Builder)
当您想以编程方式创建视图时使用(不使用 Interface Builder)
回答by alamodey
Just adding some code examples to demonstrate what NilObject said:
只需添加一些代码示例来演示 NilObject 所说的内容:
- (void)loadView
{
// create and configure the table view
myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.scrollEnabled = NO;
self.view = myTableView;
self.view.autoresizesSubviews = YES;
}
- (void)viewDidLoad
{
self.title = @"Create group";
// Right menu bar button is to Save
UIBarButtonItem *saveButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = saveButtonItem;
[saveButtonItem release];
}
回答by futureelite7
To prevent an infinite loop from happening when you read self.view, call the class' super implementation when you load a view. The super implementation will allocate a new UIView for you.
为了防止在读取 self.view 时发生无限循环,请在加载视图时调用类的超级实现。超级实现将为您分配一个新的 UIView。
- (void) loadView {
[super loadview];
// init code here...
[self.view addSubView:mySubview1]; //etc..
}
回答by Josip B.
The easiest way to use loadView is to make some type of base view controller, like MyBaseViewController which is subclass of UIViewController. In it's loadView method create view in this way:
使用 loadView 的最简单方法是制作某种类型的基本视图控制器,例如 MyBaseViewController,它是 UIViewController 的子类。在它的 loadView 方法中,以这种方式创建视图:
-(void) loadView {
if ([self viewFromNib]) {
self.view = [self viewFromNib];
} else {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
}
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor whiteColor];
}
And when you need to make some view controller you just use subclass of MyBaseViewController and in it's loadView controller you just call [super loadView] like this
当你需要制作一些视图控制器时,你只需使用 MyBaseViewController 的子类,在它的 loadView 控制器中你只需像这样调用 [super loadView]
//sucblass loadView
-(void) loadView {
[super loadView];
//rest of code like this..
UILabel *myLabel = [[UILabel alloc] initWithFrame:myFrame];
[self.view addSubview:myLabel];
[myLabel release];
}
回答by Dulguun Otgon
loadView()
is called when your controller is asked to create its self.view
. You can do it by yourself like
loadView()
当您的控制器被要求创建其self.view
. 你可以自己做
self.view = [UIView alloc] init...];
Or your controller's parent UIController class has already a method name -loadView()
which initializes your self.view into blank view. Then you can call
或者您的控制器的父 UIController 类已经有一个方法名称-loadView()
,它将您的 self.view 初始化为空白视图。然后你可以打电话
[super loadView];
I really recommend the second approach as it encourages the inheritance. Only if your view controller is not directly inherited from UIViewController.
我真的推荐第二种方法,因为它鼓励继承。仅当您的视图控制器不是直接从 UIViewController 继承时。
回答by Gulsan Borbhuiya
The definition given by Apple on viewDidLoad mentioned that it is called after the controller's view is loaded into memory. To put it in a simple term, it is the first method that will load.
苹果在 viewDidLoad 上给出的定义中提到在控制器的视图加载到内存后调用。简单来说,它是第一个加载的方法。
You might be thinking under what condition will this method being fully utilized? The answer is, basically whatever you wanted the app to load first. For instance, you might want a different background color, instead of white, you could perhaps choose blue.
您可能会想,这种方法在什么条件下才能被充分利用?答案是,基本上无论您希望应用程序首先加载什么。例如,您可能需要不同的背景颜色,而不是白色,您可以选择蓝色。