开发者

Problem with NSTimer

开发者 https://www.devze.com 2023-01-13 03:49 出处:网络
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.

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)viewDi开发者_StackOverflowdUnload {
}

- (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,


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


- (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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消