2010-08-25 30 views
3

編輯2:爲什麼只有進度得到更新的「doSomething」方法,而不是point0?NSTimer問題

編輯:用我的代碼。我知道我必須忽略某些東西,但我無法找到它。

我在寫一個iphone應用程序,它使用NSTimer來完成一些任務。在程序中,我無法獲得NSTimer循環內更新的變量值。這是我的代碼。

接口文件

進口

@interface TestNSTimerViewController : UIViewController { 
    IBOutlet UIProgressView *progress; 
    IBOutlet UIButton *button; 
    IBOutlet UILabel *lable1; 
    IBOutlet UILabel *lable2; 
    NSTimer *timer; 
    float point0; 
} 

@property (nonatomic, retain) UIProgressView *progress; 
@property (nonatomic, retain) UIButton *button; 
@property (nonatomic, retain) NSTimer *timer; 
@property (nonatomic, retain) UILabel *lable1; 
@property (nonatomic, retain) UILabel *lable2; 

- (IBAction)buttonClicked:(id)sender; 

@end 

實現文件

#import "TestNSTimerViewController.h" 

@implementation TestNSTimerViewController 

@synthesize progress; 
@synthesize button; 
@synthesize lable1; 
@synthesize lable2; 
@synthesize timer; 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidUnload { 
} 

- (void)buttonClicked:(id)sender { 
    point0 = 1.0f; 
    lable1.text = [NSString stringWithFormat:@"%3.1f",point0]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
              target:self selector:@selector(doSomething) userInfo:nil repeats:YES]; 
    lable2.text = [NSString stringWithFormat:@"%3.1f",point0]; 
} 

- (void)doSomething { 
    progress.progress = progress.progress+0.1; 
    point0 = 2.0f; 
    if (progress.progress == 1.0) { 
     [timer invalidate]; 
    } 
} 
- (void)dealloc { 
    [button release]; 
    [progress release]; 
    [lable1 release]; 
    [lable2 release]; 
    [timer release]; 
    [super dealloc]; 
} 

@end 

中的NSTimer循環後,我檢查point0的價值。它沒有把值改爲2.3。代碼有什麼問題?

謝謝

+0

我不會合成或將它的屬性也浮動,我們可以看到更多的代碼? – 2010-08-25 05:29:07

+0

在我啓動計時器的方法中,我註釋掉了所有內容,但是這兩行和行都打印出了point0的值。 – UpperLuck 2010-08-25 05:45:58

+0

哦..我也刪除了浮點數的綜合和屬性,並沒有幫助。 – UpperLuck 2010-08-25 05:46:36

回答

0

Reference document

一旦被調度上的運行循環,在指定的時間間隔定時器觸發,直到它是無效的。非重複定時器在其觸發後立即失效。但是,對於重複計時器,您必須通過調用其無效方法來使計時器對象無效。調用此方法請求從當前運行循環中刪除定時器;因此,應始終從安裝了計時器的同一線程中調用invalidate方法。使定時器無效立即禁用它,以便它不再影響運行循環。運行循環會在無效方法返回之前或之後的某個時間點刪除並釋放計時器。一旦無效,定時器對象不能被重用。

您使用的計時器是一個重複計時器,所以您不應該使其無效。或者使用下面的行,因爲每次單擊按鈕時都需要觸發計時器。我已將重複參數設置爲NO。

timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
             target:self selector:@selector(doSomething) userInfo:nil repeats:NO]; 
+0

我試圖將重複設置爲NO,並且進度條停在0.2。我的問題是,如果進度得到更新,爲什麼不點0。 – UpperLuck 2010-08-25 21:46:23

0
- (void)buttonClicked:(id)sender { 
point0 = 1.0f; 
lable1.text = [NSString stringWithFormat:@"%3.1f",point0]; 
[self.timer invalidate];  
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
             target:self selector:@selector(doSomething) userInfo:nil repeats:NO]; 
lable2.text = [NSString stringWithFormat:@"%3.1f",point0]; 

}

然後在doSometing功能:

- (void)doSomething { 
progress.progress = progress.progress+0.1; 
point0 = 2.0f; 
if (progress.progress < 1.0) { 
    [self.timer invalidate]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
             target:self selector:@selector(doSomething) userInfo:nil repeats:NO]; 



} 
} 

我想你應該在某些時候進度變量復位。

+0

我試過你的代碼沒有任何運氣。 – UpperLuck 2010-08-25 21:44:13

0

我找到了答案。 label2.text行在NSTimer完成運行循環之前執行。我需要重寫我的代碼,以便它等到NSTimer完成運行循環。