2013-03-07 2 views
3

문제가 있습니다. 어쩌면 내가 잘못된 곳이나 무언가에 무언가를 가지고있을 수도 있지만, 당황 할 수는 없다. 어떤 도움을 주시면 감사하겠습니다.UILabel을 프로그래밍 방식으로 현재 시간에 추가

단순히 시계를 프로그래밍 방식으로 만들어 내 서베이에서만 표시하려고합니다.

내가 만든 하위보기에 표시하려는 업데이터 - (void)와 함께 타이머를 설정했습니다. IBOutlet을 추가하지 않고 그냥 내 스토리 보드에 연결하는 이유는 프로그래밍 방식으로 하위 뷰를 작성하기 때문에 코드만으로 모든 작업을 수행하려고합니다.

알 수없는 식별자 'rightLabel'을 사용하는 'updateTimer'라벨에 오류가 있습니다. 제대로 작동하지 않습니다. HA HA - 어떤 도움을 주시면 대단히 감사하겠습니다! 당신은 rightLabel은 속성 (또는 인스턴스 변수) 선언이없는 .H

@interface DemoRootViewController : UIViewController <PaperFoldViewDelegate> { 
    NSTimer *timer; 
} 
@property (nonatomic, strong) UIView *rightView; 
-(void)updateTimer; 

하는 .m

- (id)init 
{ 
    self = [super init]; 
    if (self) { 

     //Timer Setup 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 

     //PaperFold Setup 
     _paperFoldView = [[PaperFoldView alloc] initWithFrame:CGRectMake(0,0,[self.view bounds].size.width,[self.view bounds].size.height)]; 
     [_paperFoldView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; 
     [self.view addSubview:_paperFoldView]; 

     //Setup Subview 
     _rightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,240,[self.view bounds].size.height)]; 

     //Setup UILabel 
     UILabel *rightLabel = [[UILabel alloc] initWithFrame:_rightView.frame]; 
     [_rightView addSubview:rightLabel]; 

     //Using PaperFold Framework to Add the Subview (this works fine) 
     [_paperFoldView setRightFoldContentView:_rightView foldCount:2 pullFactor:1]; 

    } 
    return self; 
} 


-(void)updateTimer { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"hh:mm:ss"]; 
    //This is where I get my error "Use of Undeclared Identifier 'rightLabel' 
    rightLabel.text = [formatter stringFromDate:[NSDate date]]; 
} 

@end 

답변

3

. 그냥 아래의 코드로하는 .m을 변경

하는 .m @interface DemoRootViewController() 부분은 클래스 확장이라고

@interface DemoRootViewController() 
@property (nonatomic, strong) UILabel *rightLabel; 
@property (nonatomic, strong) NSDateFormatter *dateFormatter; 
@end 

@implementation DemoRootViewController 
- (id)init 
{ 
    self = [super init]; 
    if (self) { 

     //Timer Setup 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 

     //PaperFold Setup 
     _paperFoldView = [[PaperFoldView alloc] initWithFrame:CGRectMake(0,0,[self.view bounds].size.width,[self.view bounds].size.height)]; 
     [_paperFoldView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; 
     [self.view addSubview:_paperFoldView]; 

     //Setup Subview 
     _rightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,240,[self.view bounds].size.height)]; 

     //Setup UILabel 
     self.rightLabel = [[UILabel alloc] initWithFrame:_rightView.frame]; 
     [_rightView addSubview:self.rightLabel]; 

     //Using PaperFold Framework to Add the Subview (this works fine) 
     [_paperFoldView setRightFoldContentView:_rightView foldCount:2 pullFactor:1]; 

     //Formatter setup 
     self.formatter = [[NSDateFormatter alloc] init]; 
     [self.formatter setDateFormat:@"hh:mm:ss"]; 
    } 
    return self; 
} 


-(void)updateTimer { 
    self.rightLabel.text = [self.formatter stringFromDate:[NSDate date]]; 
} 

@end 

. 기본적으로 "개인"속성을 허용하므로 헤더 파일을 통해 노출되지 않습니다. 공개하고 싶다면 두 속성 정의를 .h 파일에 넣기 만하면됩니다.

+0

클래스 확장은 개인 인스턴스 변수 용이 아닙니다. 그것들은 제 대답과 같이'@ implementation' 블록에 있습니다. 클래스 확장은 (지금 불필요한) 전달 메소드 선언과 전용 속성을위한 것입니다. 또한 클래스가 프로토콜을 준수 함을 나타내는 데 사용될 수도 있습니다. – rmaddy

+0

Ned에게 감사드립니다. 나는 그것을 친구 감사드립니다. –

+0

멋진 rmaddy - 그것에 대해 몰랐습니다. 내 대답을 업데이트했습니다. 다행히 도와 줘서, 션. – Ned

2

레이블을 업데이트하려면 레이블 변수를 인스턴스 변수로 만드십시오. 그런 다음 init 방법으로 생성하고 updateTimer 방법으로 액세스 할 수 있습니다.

또한 날짜 형식화 프로그램을 인스턴스 변수로 만들어 init 메서드에서 한 번만 만들어야합니다. 2 초마다 새로운 날짜 포맷터를 작성할 필요가 없습니다.

timer은 개인 인스턴스 변수 여야하므로 .h 파일에서 제거하고 .m 파일을 넣으십시오.

@implementation DemoRootViewController { 
    NSTimer *timer; 
} 

도 거기 rightLabelformatter 인스턴스 변수를 추가합니다.

또한 .h 파일에서 updateTimer의 선언을 제거하십시오. 이것은 .m 파일의 구현에서만 사용되는 개인 메소드입니다. .h 파일에 추가하면 다른 클래스가 메소드를 호출 할 수 있습니다.

0

또 다른 방법으로 rightLabel을 사용하고 있기 때문에 .h 파일에서 UILabel * rightLabel을 선언해야합니다.

관련 문제