NSTimer 的问题
编辑2:为什么在“doSomething”方法中只更新了进度而不是point0?
编辑:使用我拥有的代码。我知道我必须忽略一些东西,但我就是找不到它。
我正在编写一个 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。代码有什么问题吗?
谢谢你,
Edit2: Why only progress got updated in "doSomething" method but not point0?
Edit: with the code I have. I know I must overlook something, but I just could not find it.
I am writing an iphone app which uses NSTimer to do some tasks. In the program, I could not get the updated value of the variable updated inside NSTimer loop. Here is my code.
Interface File
import
@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
Implementation file
#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
After the NSTimer loop, I checked the value of point0. It did not change the value to 2.3. What's wrong with the code?
Thank you,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自参考文档
您使用的计时器是重复计时器,因此您根本不应该使其无效。或者使用以下行,因为每次单击按钮时都需要触发计时器。我已将重复参数设置为“否”。
From the Reference document
The timer you use is a Repeating timer, so you should not invalidate it at all. Or use the following Line, because the timer needs to fired each time the button is clicked. I have set the repeat parameter to NO.
然后
在 doSometing 函数中:
我认为你应该在某个时候重置进度变量。
}
Then in doSometing function:
I think you should reset the progress variable at some point.
我找到了答案。 label2.text 行在 NSTimer 完成运行循环之前执行。我需要重写我的代码,以便它等到 NSTimer 完成运行循环。
I found the answer. label2.text line is executed before NSTimer finishes the run loop. I need to rewrite my code such that it waits until NSTimer finishes the run loop.