2010-08-25 33 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完成运行循环。