Programing

iPhone SDK : loadView와 viewDidLoad의 차이점은 무엇입니까?

lottogame 2020. 6. 25. 08:05
반응형

iPhone SDK : loadView와 viewDidLoad의 차이점은 무엇입니까?


iPhone 앱에서 뷰 및 뷰 컨트롤러를 사용할 때 loadView와 viewDidLoad의 차이점을 설명 할 수 있습니까?

내 개인적인 맥락은 코드에서 모든보기를 작성한다는 것입니다. 인터페이스 빌더를 사용하지 않을 것입니다.

init 코드를 loadView에 추가 할 때 무한 스택 추적으로 끝나는 경우가 많으므로 일반적으로 viewDidLoad에서 모든 자식 뷰 건물을 수행하지만 각각이 실행될 때 나에게 분명하지 않습니다. init 코드를 넣기에 더 적합한 장소는 무엇입니까? 완벽한 것은 초기화 호출의 간단한 다이어그램입니다.

감사!


내가 한 일 때문에 여기에서 문제가 무엇인지 짐작할 수 있습니다.

init 코드를 loadView에 추가하면 무한 스택 추적으로 끝나는 경우가 종종 있습니다.

-loadView에서 self.view를 읽지 마십시오. 설정 하지 않는, 그것을 얻기 를.

self.view 속성 접근 자는 뷰가 현재로드되지 않은 경우 -loadView를 호출 합니다. 무한 재귀가 있습니다.

Apple의 Pre-Interface-Builder 예제에서 설명한 것처럼 -loadView에서 프로그래밍 방식으로 뷰를 작성하는 일반적인 방법은 다음과 같습니다.

UIView *view = [[UIView alloc] init...];
...
[view addSubview:whatever];
[view addSubview:whatever2];
...
self.view = view;
[view release];

그리고 IB를 사용하지 않았다고 비난하지 않습니다. 나는 모든 Instapaper에 대해이 방법을 고수했으며 IB의 복잡성, 인터페이스 문제 및 예기치 않은 비하인드 행동을 다루는 것보다 훨씬 더 편합니다.


loadViewUIViewController실제로 뷰를로드하고 view속성에 할당 하는 방법입니다 . UIViewController프로그래밍 방식으로 view속성을 설정하려는 경우 하위 클래스 가 재정의 하는 위치이기도합니다 .

viewDidLoad뷰가로드되면 호출되는 메소드입니다. loadView가 호출 된 후 호출됩니다. 뷰가 일단로드되면 추가 초기 설정을 수행하는 코드를 무시하고 삽입 할 수있는 위치입니다.


viewDidLoad()

NIB에서보기를로드 할 때 사용되며 시작 후 사용자 정의를 수행하려고합니다.

LoadView()

프로그래밍 방식으로보기를 작성하려고 할 때 사용됩니다 (인터페이스 빌더를 사용하지 않고).


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];
}

self.view를 읽을 때 무한 루프가 발생하지 않도록하려면 뷰를로드 할 때 클래스의 수퍼 구현을 호출하십시오. 슈퍼 구현은 새로운 UIView를 할당합니다.

- (void) loadView {
[super loadview];

// init code here...

[self.view addSubView:mySubview1]; //etc..

}

loadView를 사용하는 가장 쉬운 방법은 UIViewController의 하위 클래스 인 MyBaseViewController와 같은 일부 유형의 기본 뷰 컨트롤러를 만드는 것입니다. 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];
}

뷰 컨트롤러를 만들어야 할 때는 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];
}

loadView()컨트롤러가를 생성하라는 메시지가 표시되면 호출됩니다 self.view. 당신은처럼 혼자서 할 수 있습니다

self.view = [UIView alloc] init...];

또는 컨트롤러의 부모 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.


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.

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.

참고URL : https://stackoverflow.com/questions/573958/iphone-sdk-what-is-the-difference-between-loadview-and-viewdidload

반응형