NSTimer 的问题

发布于 2024-09-15 20:55:23 字数 1837 浏览 2 评论 0原文

编辑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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任谁 2024-09-22 20:55:23

来自参考文档

一旦调度到运行循环上,计时器就会按照指定的时间间隔触发,直到其失效为止。非重复计时器在触发后立即失效。但是,对于重复计时器,您必须通过调用其 invalidate 方法自行使计时器对象无效。调用此方法请求从当前运行循环中删除计时器;因此,您应该始终从安装计时器的同一线程调用 invalidate 方法。使定时器无效会立即禁用它,使其不再影响运行循环。然后,运行循环会在 invalidate 方法返回之前或稍后的某个时刻删除并释放计时器。一旦失效,定时器对象就不能被重复使用。

您使用的计时器是重复计时器,因此您根本不应该使其无效。或者使用以下行,因为每次单击按钮时都需要触发计时器。我已将重复参数设置为“否”。

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

From the Reference document

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes and releases the timer, either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

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.

timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
                                         target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
枕花眠 2024-09-22 20:55:23
- (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];



}
}

我认为你应该在某个时候重置进度变量。

- (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];

}

Then in doSometing function:

- (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];



}
}

I think you should reset the progress variable at some point.

复古式 2024-09-22 20:55:23

我找到了答案。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文