1

我有一个简单的应用程序,它在 appDelegate 中有一个 NSTimer 对象,所有视图都可以访问它。应用程序的结构是一个 UINavigationController。当我触发 NSTimer 对象时,我的 UILabel 正在使用正确的倒计时功能进行更新,但是当我返回 rootViewController 并返回倒计时计时器视图时,我的 UILabel 正在使用当前倒计时时间进行更新,但没有后续更新UILabel 发生。我错过了什么?我已经完成了确保 UILabel 对象不是 nil 的研究,我在 viewDidAppear 方法上调用了该函数,但似乎没有任何效果!这是代码:

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
}
@property (nonatomic, retain) NSTimer *countdownTimer;

AppDelegate.m

@implementation AppDelegate

@synthesize countdownTimer;

CountdownTimerViewController.h

#import "AppDelegate.h"
enter code here
@interface CountdownTimerViewController : UIViewController {
enter code here
AppDelegate *appdelegate;

}

@property (strong, nonatomic) IBOutlet UILabel *labelCountdownTimer;
@property (strong, nonatomic) IBOutlet UIButton *buttonStartTimer;
@property (strong, nonatomic) IBOutlet UIButton *buttonStopTimer;

- (IBAction)startTimer:(id)sender;
- (IBAction)stopTimer:(id)sender;

CountdownTimerViewController.m

@implementation CountdownTimerViewController

@synthesize labelCountdownTimer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Instatiating Appdelegate
    if(!appdelegate)
        appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

}

- (void) viewDidAppear:(BOOL)animated {

    if ([appdelegate.countdownTimer isValid]) {
        [self countDown];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Button Action Methods

- (IBAction)startTimer:(id)sender {

    [self updateCounter];

}

- (IBAction)stopTimer:(id)sender {

    [appdelegate.countdownTimer invalidate];
    labelCountdownTimer.text = @"00:00:00";

}

int countLimit=30; //seconds
NSDate *startDate;

- (void)countDown {

    if([[NSDate date] timeIntervalSinceDate:startDate] >= countLimit) {
        [appdelegate.countdownTimer invalidate];
        return;
    }
    else {            
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = -([currentDate timeIntervalSinceDate:startDate]);
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString = [dateFormatter stringFromDate:timerDate];
        NSLog(@"timeString: %@",timeString);
        NSLog(@"labelCountdownTimer: %@",labelCountdownTimer);
    labelCountdownTimer.text = timeString;
    }

}

- (void)updateCounter {

    labelCountdownTimer.text = @"00:00:00";
    startDate = [NSDate date];

    appdelegate.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                                  target:self
                                                                selector:@selector(countDown)
                                                                userInfo:nil
                                                                 repeats:YES];

}
4

3 回答 3

1

感谢大家的意见。实际上,我通过执行一个方法来解决它,该方法将在我的 AppDelegate 中检索 NSTimer 正在更新的值,因为当我离开视图并返回视图时,触发 NSTimer 的方法不再位于主线程中。只要我的 NSTimer 有效,此方法就会循环。我还设置了延迟,允许 UI 更新值,然后再次执行该方法。这是代码,以防它帮助遇到类似问题的人。我从chandan提供的建议中得到了这个想法,谢谢!

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

}

@property (nonatomic, retain) NSTimer *countdownTimer;
@property (nonatomic, retain) NSString *timeString;

AppDelegate.m

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

}

@property (nonatomic, retain) NSTimer *countdownTimer;
@property (nonatomic, retain) NSString *timeString;

CountdownTimerViewController.h

@interface CountdownTimerViewController : UIViewController {

AppDelegate *appdelegate;

}

@property (strong, nonatomic) IBOutlet UILabel *labelCountdownTimer;

@property (strong, nonatomic) IBOutlet UIButton *buttonStartTimer;
@property (strong, nonatomic) IBOutlet UIButton *buttonStopTimer;

- (IBAction)startTimer:(id)sender;
- (IBAction)stopTimer:(id)sender;

CountdownTimerViewController.m

@implementation CountdownTimerViewController

@synthesize labelCountdownTimer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view.

    //Instatiating Appdelegate
    if(!appdelegate)
        appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

}

- (void) viewDidAppear:(BOOL)animated {

    if ([appdelegate.countdownTimer isValid]) {
        [self updateLabel];
    } else {
        labelCountdownTimer.text = @"00:00:00";
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Button Action Methods

- (IBAction)startTimer:(id)sender {

    [self updateCounter];

}

- (IBAction)stopTimer:(id)sender {

    [appdelegate.countdownTimer invalidate];
    labelCountdownTimer.text = @"00:00:00";

}

int countLimit=30; //seconds
NSDate *startDate;

- (void)updateCounter {

    labelCountdownTimer.text = @"00:00:00";
    startDate = [NSDate date];

    appdelegate.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                                  target:self
                                                                selector:@selector(countDown)
                                                                userInfo:nil
                                                                 repeats:YES];

}

  - (void)countDown {

    if([[NSDate date] timeIntervalSinceDate:startDate] >= countLimit) {
        [appdelegate.countdownTimer invalidate];
        return;
    }
    else {            
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = -([currentDate timeIntervalSinceDate:startDate]);
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    appdelegate.timeString = [dateFormatter stringFromDate:timerDate];
    labelCountdownTimer.text = appdelegate.timeString;
    }

} 


- (void) updateLabel {

    if ([appdelegate.countdownTimer isValid]) {
        labelCountdownTimer.text = appdelegate.timeString;
        [self performSelector:@selector(updateLabel) withObject:nil afterDelay:0.05];
    } 

}
于 2013-05-07T00:18:23.123 回答
0

像这样键入铸造:

appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

实际上不会将 UIApplicationDelegate 放入您的 AppDelegate 类并添加您的额外参数。因此,此变量中将没有指向计时器的指针。

您需要一种不同的方法来存储计时器指针。

于 2013-05-06T05:42:17.490 回答
0

尝试在主线程上更新 UILabel 上的文本。有时 UILabel 中的更新在后台线程上不起作用。

- (void) viewDidAppear:(BOOL)animated 
{
     if ([appdelegate.countdownTimer isValid]) 
     {
          [self performSelectorOnMainThread:@selector(countDown) withObject:nil waitUntilDone:NO];
     } 
}

如果您的 appdelegate 对象工作正常并且 UILabel 正在使用当前倒计时时间进行更新,但没有对 UILabel 进行后续更新,则将 UI 更改应用到主线程上

- (void)countDown {

 [self performSelectorOnMainThread:@selector(changeCountDownValue) withObject:nil waitUntilDone:NO];
}

- (void)changeCountDownValue
{
    if([[NSDate date] timeIntervalSinceDate:startDate] >= countLimit) {
        [appdelegate.countdownTimer invalidate];
        return;
    }
    else {            
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = -([currentDate timeIntervalSinceDate:startDate]);
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString = [dateFormatter stringFromDate:timerDate];
        NSLog(@"timeString: %@",timeString);
        NSLog(@"labelCountdownTimer: %@",labelCountdownTimer);
    labelCountdownTimer.text = timeString;
    }

}

请仔细检查 NSTimer 对象。它应该适用于 UILabel 更新。如果仍然出现任何问题,请告诉我。

于 2013-05-06T06:13:04.267 回答