編輯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。代碼有什麼問題?
謝謝
我不會合成或將它的屬性也浮動,我們可以看到更多的代碼? – 2010-08-25 05:29:07
在我啓動計時器的方法中,我註釋掉了所有內容,但是這兩行和行都打印出了point0的值。 – UpperLuck 2010-08-25 05:45:58
哦..我也刪除了浮點數的綜合和屬性,並沒有幫助。 – UpperLuck 2010-08-25 05:46:36