2009-08-01 2 views
0

(우리는 사용자 정의 UIViewController 하위 클래스의 코드에 대해 이야기하고 있습니다. 그런데 IB를 사용하지 않습니다.) 그렇기 때문에 self.view 멤버를 다음과 같이 설정했습니다. - (void) loadView, 그리고 나서 내 컨트롤과 뷰 및 무엇이든 (void) viewDidLoad를 만든 다음 하위 뷰에 추가합니다. 컨트롤이 회원이 아닌 경우 내가 만든 방법에 로컬로 놓으면, 이것은 내가 할 방법은 다음과 같습니다 (A UILabel의로)iPhone Dev - 어떤 방법으로 하위 뷰로 멤버를 추가해야합니까?

- (void)viewDidLoad { 
    UILabel *localLabel = [[UILabel alloc] initWithFrame:CGRectMake(81, 384, 148, 21)]; 
    localLabel.text = @"I'm a Label!"; 
    localLabel.AutoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
            UIViewAutoresizingFlexibleRightMargin | 
            UIViewAutoresizingFlexibleBottomMargin); 

    [self.view addSubview:localLabel]; 
    [localLabel release]; 
    [super viewDidLoad]; 
} 

그게 난을 만들 것입니다 방법의 단지 예 로컬로 레이블을 지정하고, 속성을 설정하고, 하위보기에 추가하고 릴리스합니다. 그러나 멤버로,이 수행

UILabel *lblMessage; 
... 
@property (nonatomic, retain)UILabel *lblMessage; 
... 
- (void)viewDidLoad { 
    UILabel *localMessage = [[UILabel alloc] initWithFrame:CGRectMake(81, 384, 148, 21)]; 
    localMessage.text = @"I'm a Label!"; 
    localMessage.AutoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleBottomMargin); 
    self.lblMessage = localMessage; 
    [localMessage release]; 

    [self.view addSubview:lblMessage]; 
    [super viewDidLoad]; 
} 

을 그러나 나는 또한 나의 시작 아이폰 3 개발에 그런

... 
- (void)viewDidLoad { 
    UILabel *localMessage = [[UILabel alloc] initWithFrame:CGRectMake(81, 384, 148, 21)]; 
    localMessage.text = @"I'm a Label!"; 
    localMessage.AutoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
             UIViewAutoresizingFlexibleRightMargin | 
             UIViewAutoresizingFlexibleBottomMargin); 
    self.lblMessage = localMessage; 

    [self.view addSubview:localMessage]; 
    [localMessage release]; 
    [super viewDidLoad]; 
} 

일을 본 적이 : SDK를 책을 탐험. 지역 변수를 추가 한 다음 해제하십시오. 어느 것이해야합니까? 그것은 전혀 중요합니까?

답변

1

lblMessage이 보유 속성 인 경우 (종종 사실 임) 기능적 차이가 없습니다. 그렇지 않으면 release-before-addSubview는 할당 취소 된 객체를 하위보기로 추가하려고하기 때문에 버그입니다. 여기

이 유지되는 재산 lblMessage 가정, localMessage에 참조 횟수의 빠른 연습입니다 :
UILabel *localMessage = [[UILabel alloc]... // retainCount is now 1 
// Set up localMessage. If you release'd now, you'd dealloc the object. 
self.lblMessage = localMessage; // retainCount is now 2 
// You can safely call release now if you'd like. 
[self.view addSubview:localMessage]; // retainCount is now 3. 
[localMessage release]; // retainCount is now 2. 

당신은 2에서 종료합니다 retainCount을 원 효과적으로 해당 개체에 대한 2 참조가 있기 때문에 - 귀하의 회원 포인터 lblMessage 및 다른 보유 포인터 self.view.

0

구성원 인 레이블과 로컬 범위 레이블은 서로 참조되므로 동일한 개체이므로 어떤 방식 으로든 상관 없습니다. 로컬은 없으며 직접 레이블을 초기화 할 것입니다.

+0

lblMessage가 보유 속성이므로 로컬을 만들어야하므로 self.lblMessage = [[UILabel alloc] initWithFrame : ..]; obj reference 2를 만들 것입니다. – mk12

+0

정확하지 않은 Ur 문장, countwill은 여전히 ​​I Blive입니다. – Daniel

관련 문제