2012-05-31 23 views
0

我正在嘗試計算用戶將手指放在按鈕 上的時間,以秒爲單位顯示label.text ...(up到10秒) 一切工作正常,但label.text更新的動作 (我只有一個按鈕和一個標籤的結尾......)IBAction方法中的標籤文本不會更改

我的頭:

#import <UIKit/UIKit.h> 
#import <CoreFoundation/CFData.h> 
@interface TwisterViewController : UIViewController { 
    UILabel *label; 
    CFTimeInterval presstime; 
} 

@property(nonatomic,retain) IBOutlet UILabel *label; 
-(IBAction) fingeron; 

@end 

我m-file

#import "TwisterViewController.h" 
@implementation TwisterViewController 
@synthesize label; 
-(IBAction) fingeron 
{ 
    int holdtime; 
    presstime = CFAbsoluteTimeGetCurrent(); 
    holdtime = (int) (CFAbsoluteTimeGetCurrent() - presstime); 
    NSString *holdtimetext; 
    while (holdtime <= 10) { 
    holdtimetext = [NSString stringWithFormat:@"%d", holdtime]; 
    label.text = holdtimetext; 
    holdtime = CFAbsoluteTimeGetCurrent() - presstime; 
    } 
    presstime = 0; 
} 

所以label.text是空的,持續9秒,並在和動作的顯示「10」

回答

0

克里特計時器此

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(fingerOn) userInfo:nil repeats: YES]; 

然後用自己的方法

-(IBAction) fingeron 
    { 
     int holdtime; 
    presstime = CFAbsoluteTimeGetCurrent(); 
    holdtime = (int) (CFAbsoluteTimeGetCurrent() - presstime); 
    NSString *holdtimetext; 
    while(holdtime <= 10) 
    { 
     holdtimetext = [NSString stringWithFormat:@"%d", holdtime]; 
     label.text = holdtimetext; 
     holdtime = CFAbsoluteTimeGetCurrent() - presstime; 
    } 
    presstime = 0; 
} 
+0

我把它放在哪裏? couse它沒有做任何事情 – user1427972

+0

試試這個ViewDidload或ViewWillAppear方法的ViewController –