3

問題があります。多分私は間違った場所か何かに何かを持っているかもしれませんが、私はそれを理解することができません! どんな助けでも大歓迎です。

サブビューにのみ表示される時計をプログラムで作成しようとしています。

作成したサブビューに表示したい updater -(void) と一緒にタイマーをセットアップしました。IBOutletを追加せず、ストーリーボードに接続するだけの理由であるプログラムでサブビューを構築しています-コードだけですべてをやろうとしています。

updateTimer ラベルに、宣言されていない識別子「rightLabel」の使用というエラーが表示されますが、うまくいきません。HA HA - どんな助けでも大歓迎です!

.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
4

3 に答える 3

3

rightLabelプロパティ (またはインスタンス変数) として宣言していません。.m を以下のコードに変更するだけです。

.m

@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

この@interface DemoRootViewController ()部分はクラス拡張と呼ばれます。基本的に「プライベート」プロパティを許可するため、ヘッダー ファイルを通じて公開されません。それらを公開したい場合は、2 つのプロパティ定義を .h ファイルに入れるだけです。

于 2013-03-07T23:29:19.943 に答える
2

ラベルを更新したいので、ラベル変数をインスタンス変数にします。次に、メソッドで作成し、initメソッドでアクセスできますupdateTimer

また、日付フォーマッタをインスタンス変数にして、メソッドで一度だけ作成する必要があるようにしますinit。0.5 秒ごとに新しい日付フォーマッタを作成する必要はありません。

あなたtimerはプライベートインスタンス変数でなければならないので、.hファイルから削除して.mファイルに入れます。

@implementation DemoRootViewController {
    NSTimer *timer;
}

rightLabelそこに theとformatterivar も追加します。

updateTimerまた、 .h ファイルからの宣言を削除します。これは、.m ファイル内の実装によってのみ使用されるプライベート メソッドです。.h ファイルに追加すると、他のクラスがメソッドを呼び出すことができます。

于 2013-03-07T23:30:00.513 に答える
0

別のメソッドでもrightLabelを使用しているため、.hファイルでUILabel*rightLabelを宣言する必要があります。

于 2013-03-08T04:09:34.207 に答える