에리카 사둔 (Erica Sadun)의 책 "The iPhone Developer 's Cookbook"의 일부보기 코드를 시험 해보았으며 이해할 수없는 코드를 발견했습니다. 여기에는 loadView 방법에 대한 코드이다 : 그녀가있는 contentView가 해제 않는 이유왜 iPhone Developer 's Cookbook의이 코드가 작동합니까?
- (void)loadView
{
// Create the main view
UIView *contentView = [[UIView alloc] initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
// Get the view bounds as our starting point
CGRect apprect = [contentView bounds];
// Add each inset subview
UIView *subview = [[UIView alloc]
initWithFrame:CGRectInset(apprect, 32.0f, 32.0f)];
subview.backgroundColor = [UIColor lightGrayColor];
[contentView addSubview:subview];
[subview release];
}
내 질문은,하지만 다음 [contentView addSubview:subview]
에 다시 사용? self.view = contentView
보유 contentView가 있습니까?
잘못된 것처럼 보입니다. 'contentView'는 메서드가 끝날 때까지 공개되지 않으므로 아무런 문제가 발생하지는 않지만 의도적으로이 방법으로 빌드하는 이유는 상상할 수 없습니다. – kubi
아닙니다. self.view에 대한 할당은 contentView를 유지하므로, 바로 뒤에서 풀어주는 것은 객체를 만들 때 얻을 수있는 가장 가까운 방법입니다. 코코아 패러다임에서 볼 때 뷰 컨트롤러가 뷰를 유지하기 때문에 의도적으로이 방법으로 구현됩니다. –