编辑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