用于 UIView 的 iOS viewDidLoad
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10167706/
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
iOS viewDidLoad for UIView
提问by manuelBetancurt
In a ViewController, there is ViewDidLoad
to know when the VC has loaded.
在 ViewController 中,ViewDidLoad
需要知道 VC 何时加载。
For a UIView, what method do i have to use when the view loaded?
对于 UIView,加载视图时必须使用什么方法?
Would this method be called with any init?
这个方法会用任何 init 调用吗?
edit: No XIB, just programmatically.
编辑:没有XIB,只是以编程方式。
采纳答案by borrrden
If you load it from a XIB file, the awakeFromNib method will be called when it is loaded from the XIB.
如果从 XIB 文件加载它,则当它从 XIB 加载时会调用awakeFromNib 方法。
editIn the case of no XIB, you will probably have to infer it using one of the methods from the Observing View-Related Changesarea of the docs(for example, didMoveToSuperview). However, a better way is to send a message to your views from the view controller in the viewDidLoad method.
编辑在没有 XIB 的情况下,您可能必须使用文档的Observing View-Related Changes区域中的一种方法来推断它(例如,didMoveToSuperview)。但是,更好的方法是在 viewDidLoad 方法中从视图控制器向您的视图发送消息。
回答by akelec
Actualy, you have not to do anything with view controller's method viewDidLoad(), for your view's initialization. All that you want to do, you can do in view's init method. For example, in view controller's viewDidLoad(), there is some initialization code:
实际上,对于视图的初始化,您不必对视图控制器的方法 viewDidLoad() 做任何事情。所有你想做的,你都可以在视图的 init 方法中完成。例如,在视图控制器的 viewDidLoad() 中,有一些初始化代码:
- (void)viewDidLoad{
[super viewDidLoad];
// init your parameters here
}
Analogously, in your view's init method:
类似地,在您视图的 init 方法中:
- (id)initWithDelegate:(id)_delegate
{
self = [[[[NSBundle mainBundle] loadNibNamed:@"YourView" owner:self options:nil] objectAtIndex:0] retain];
if (self) {
[super init];
self.delegate = _delegate;
// init your parameters here
return self;
}
return nil;
}
Then, you create YourView from view controller like this:
然后,您从视图控制器创建 YourView,如下所示:
YourView view = [[YourView alloc] initWithDelegate:self];
[self.view addSubview:view];
[view release];
Further, things that you want to do when your view did load, you can place in layoutSubviews method in your view, like this:
此外,当你的视图加载时你想要做的事情,你可以在你的视图中放置 layoutSubviews 方法,如下所示:
-(void)layoutSubviews{
[super layoutSubviews];
// init your parameters here, like set up fonts, colors, etc...
}
I think, that is what you need.
我想,这是你所需要的。
Cheers!
干杯!
回答by davidrynn
You can use willMove(toSuperview newSuperview: UIView?)
您可以使用 willMove(toSuperview newSuperview: UIView?)
import UIKit
final class myView: UIView {
override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
//Do stuff here
}
}
回答by fatihyildizhan
Swift 2:
斯威夫特 2:
import UIKit
class myView: UIView {
override func layoutSubviews() {
print("run when UIView appears on screen")
// you can update your label's text or etc.
}
}
回答by vedrano
I had similar problem and found relative easy solution. Idea is to send viewDidLoad to every child view at right time and overload that method on class of your interest.
我遇到了类似的问题,并找到了相对简单的解决方案。想法是在正确的时间将 viewDidLoad 发送到每个子视图,并在您感兴趣的类上重载该方法。
To do that add this parts of code in your classes...
为此,在您的课程中添加这部分代码...
// UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view viewDidLoad];
}
// UIView+Loading.m
#import < UIKit/UIKit.h>
@implementation UIView (Loading)
- (void)viewDidLoad
{
for (UIView* subview in self.subviews)
[subview viewDidLoad];
}
@end
// UIView+Loading.h
#import < UIKit/UIKit.h>
@interface UIView (Loading)
- (void)viewDidLoad;
@end
// UIView_CustomImplementation
- (void)viewDidLoad
{
NSLog(@"Do whatever you want to do in custom UIView after method viewDidLoad on UIViewController was called");
}
回答by Tranz
As far as I know, I don't think there's such a method except for the init. Usually I do the preparation in the init method. You may create your own viewDidLoad
method and call it manually. But most of time, UIView is managed by it's view controller, so that view controller should know when the view is loaded, if you want to config the view, you may do it in the view controller. By the way, the viewDidLoad
method is not always called.
据我所知,我认为除了init之外没有这样的方法。通常我在init方法中做准备。您可以创建自己的viewDidLoad
方法并手动调用它。但是大多数时候,UIView 是由它的视图控制器管理的,因此视图控制器应该知道何时加载视图,如果要配置视图,可以在视图控制器中进行。顺便说一下,该viewDidLoad
方法并不总是被调用。
回答by thebarcodeproject
It doesn't quite work like this.
它不是这样工作的。
- (void)viewDidLoad
is not called when the view controller is loaded; it is called when the view controller's view is loaded.
- (void)viewDidLoad
加载视图控制器时不调用;它在视图控制器的视图加载时被调用。
So you can create the UIViewController, and the only methods that will be called are the init methods used to initialise it. The view will not be created, the -(void)viewDidLoad method is not called, etc.
因此,您可以创建 UIViewController,并且将调用的唯一方法是用于初始化它的 init 方法。不会创建视图,不会调用 -(void)viewDidLoad 方法等。
Then when something else asks the view controller for its view, via:
然后当其他东西向视图控制器询问它的视图时,通过:
viewController.view;
The view controller then calls:
然后视图控制器调用:
- (void)loadView; // This is where you put your own loading view information, and set the viewController's self.view property here.
Followed by:
其次是:
- (void)viewDidLoad;
View Did Load is a separate method so you don't have to interrupt the actual loadView method, and the complex view loading options. Subclassing and overriding the loadView method when using nibs etc can result in problems when developers aren't sure what Apple is doing and what their best practices are, so it was smart for Apple to separate the method out.
View Did Load 是一个单独的方法,因此您不必中断实际的 loadView 方法和复杂的视图加载选项。当开发人员不确定 Apple 在做什么以及他们的最佳实践是什么时,在使用 nib 等时子类化和覆盖 loadView 方法可能会导致问题,因此 Apple 将方法分离出来是明智的。
Then, when a memory warning comes the view is released, and set to nil:
然后,当出现内存警告时,视图被释放,并设置为 nil:
- (void)viewWillUnload;
// view unloaded and set to nil.
- (void)viewDidUnload;