Problem with NSTimer

Edit2: Why was only the progress updated in the doSomething method, but not in point0?

Edit: with the code that I have. I know that I have to miss something, but I just could not find it.

I am writing an iphone application that uses NSTimer to perform some tasks. In the program, I could not get the updated value of the variable updated inside the 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. This did not change the value until 2.3. What is wrong with the code?

Thank,

+3
source share
3 answers

From the background document

, . . , invalidate. ; invalidate , . , . , invalidate, - . .

, , , . , , . NO.

timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
                                         target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
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];



}
}

, reset - .

0

I have found the answer. The label2.text line is executed before NSTimer completes the execution loop. I need to rewrite my code so that it waits until NSTimer completes the execution loop.

0
source

Source: https://habr.com/ru/post/1761458/


All Articles