UIView 용 iOS viewDidLoad
ViewController ViewDidLoad
에서 VC가 언제로드되었는지 알아야합니다.
UIView의 경우 뷰가로드 될 때 어떤 방법을 사용해야합니까?
이 메서드는 초기화로 호출됩니까?
편집 : XIB 없음, 프로그래밍 방식으로 만.
XIB 파일에서로드하는 경우 XIB에서로드 할 때 awakeFromNib 메서드가 호출됩니다.
편집 XIB가없는 경우 문서 의 뷰 관련 변경 사항 관찰 영역 (예 : didMoveToSuperview) 에있는 방법 중 하나를 사용하여 추론해야합니다 . 그러나 더 나은 방법은 viewDidLoad 메서드의 뷰 컨트롤러에서 뷰로 메시지를 보내는 것입니다.
실제로, 뷰의 초기화를 위해 뷰 컨트롤러의 메서드 viewDidLoad ()로 아무것도 할 필요가 없습니다. 원하는 모든 작업은 view의 init 메서드에서 수행 할 수 있습니다. 예를 들어, 뷰 컨트롤러의 viewDidLoad ()에는 몇 가지 초기화 코드가 있습니다.
- (void)viewDidLoad{
[super viewDidLoad];
// init your parameters here
}
유사하게, 뷰의 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;
}
그런 다음 다음과 같이 뷰 컨트롤러에서 YourView를 만듭니다.
YourView view = [[YourView alloc] initWithDelegate:self];
[self.view addSubview:view];
[view release];
또한 뷰가로드되었을 때 수행하고 싶은 작업은 다음과 같이 뷰의 layoutSubviews 메서드에 배치 할 수 있습니다.
-(void)layoutSubviews{
[super layoutSubviews];
// init your parameters here, like set up fonts, colors, etc...
}
나는 그것이 당신이 필요하다고 생각합니다.
건배!
스위프트 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.
}
}
willMove (toSuperview newSuperview : UIView?)를 사용할 수 있습니다.
import UIKit
final class myView: UIView {
override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
//Do stuff here
}
}
비슷한 문제가 있었고 비교적 쉬운 해결책을 찾았습니다. 아이디어는 viewDidLoad를 모든 자식 뷰에 적시에 보내고 관심있는 클래스에서 해당 메서드를 오버로드하는 것입니다.
이렇게하려면 클래스에이 코드 부분을 추가하십시오.
// 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");
}
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.
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.
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.
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.
Then, when a memory warning comes the view is released, and set to nil:
- (void)viewWillUnload;
// view unloaded and set to nil.
- (void)viewDidUnload;
ReferenceURL : https://stackoverflow.com/questions/10167706/ios-viewdidload-for-uiview
'your programing' 카테고리의 다른 글
영어로 오류 메시지를 표시하도록 Visual Studio 구성 (0) | 2020.12.25 |
---|---|
상단과 하단에 작업 항목이있는 Android Split Action Bar? (0) | 2020.12.25 |
공용체를 통한 유형 실행은 C99에서 지정되지 않았으며 C11에서 지정 되었습니까? (0) | 2020.12.25 |
PostgreSQL의 기본 키인 UUID가 인덱스 성능을 저하합니까? (0) | 2020.12.25 |
JDK8 및 JDK10에서 삼항 연산자의 동작 차이 (0) | 2020.12.25 |