2014-07-06 1 views
0

많은 것을 검색해 봤지만 바보 같이 잘못하고있는 것 같습니다. 내 두 번째 화면에는 많은 라벨이 있으며, 모두 동일한 속성을 가지고 있습니다. 그것이 내가 방법을 통해 그들을 만드는 이유입니다. viewDidLoad에에서 나는이 작업을 수행 : 방법이 하나메서드를 통해 생성 한 후 레이블이 누락 된 속성

[self screenTwoLabelMaker:firstNameLabel withFrame:CGRectMake(30, 200, 200, 40) withText:@"First Name"]; 

것을 :

- (UILabel *)screenTwoLabelMaker:(UILabel *)sender withFrame:(CGRect)frame withText:(NSString *)text 
{ 
    sender = [[UILabel alloc] init]; 
    sender.text = text; 
    sender.frame = frame; 
    sender.font = [UIFont systemFontOfSize:labelFontSize]; 
    sender.textColor = [UIColor grayColor]; 
    [self.scrollView addSubview:sender]; 
    return sender; 
} 

그럼이 수행

:

NSLog(@"firstNameLabel x: %f y:%f w:%f h:%f", firstNameLabel.frame.origin.x, firstNameLabel.frame.origin.y, firstNameLabel.frame.size.width, firstNameLabel.frame.size.height); 

그러나 결과이입니다

MyApp[9608:60b] firstNameLabel x: 0.000000 y:0.000000 w:0.000000 h:0.000000 

이상한 점은 레이블이 올바른 텍스트의 올바른 위치에 화면에 표시된다는 것입니다. 그래서 모든 것이 화창하고 따뜻해야합니다. 음, 태그가 포함 된 버튼과 비슷한 메소드를 호출합니다. 그 태그는 버튼 (nil)에서 절대 활성화되지 않으므로 그 버튼에 대한 추가 프로그래밍으로 인해 나를 죽일 수 있습니다. 지금 나는 그들을 모두 손으로 만든다. 내가 잘못한 것을 너희들이 알고 있니?

답변

1

메서드는 새 레이블을 반환하므로 참조를 보관할 변수에 할당해야합니다. 메소드에 변수를 전달할 필요가 없습니다. 먼저 변수를 덮어 쓰는 것입니다. 다음 코드를 사용하십시오. -

firstNameLabel = [self screenTwoMakeLabelwithFrame:CGRectMake(30, 200, 200, 40) text:@"First Name"]; 


- (UILabel *)screenTwoMakeLabelwithFrame:(CGRect)frame text:(NSString *)text 
{ 
    UILabel newLabel = [[UILabel alloc] init]; 
    newLabel.text = text; 
    newLabel.frame = frame; 
    newLabel.font = [UIFont systemFontOfSize:labelFontSize]; 
    newLabel.textColor = [UIColor grayColor]; 
    [self.scrollView addSubview:newLabel]; 
    return newLabel; 
} 
관련 문제