1

从过去几个小时开始,我一直遇到这个问题,并且做了所有我能做的搜索,但不幸的是,我没有找到任何可以解决我的问题的东西.... 场景:我在 TimerViewController 中有一个 CountDownTimer,NSTimer 和其他方法已设置在假定更新 TimerViewController 的标签的 AppDelegate 中...根据标签的设置器,我正确获取了值并在 NSLog 中显示但是,标签没有在屏幕上更新...从 AppDelegate 调用此设置器每秒,标签应该显示计时器,

- (void)setMainTimerLabel:(UILabel *)mainTimerLabel 
  {
    _mainTimerLabel = mainTimerLabel;
    NSLog(@"ValueUpdated %@",_mainTimerLabel); 
  }

我已经仔细检查了标签,它与界面正确连接,我尝试使用测试字符串从 ViewDidLoad 更新标签,标签向我显示字符串...请帮助!

编辑:AppDelegate 代码:

AppDelegate.h

@property (nonatomic, strong) TimerViewController *TimerVC;

- (void)fireTimer;

AppDelegate.m

- (void)fireTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownTimer) userInfo:nil repeats:YES];
}

- (void) countDownTimer
{
    .......
   TimerVC = [[TimerViewController alloc]init];
    self.TimerVC.mainTimerLabel = [NSString stringWithFormat:@"%02d:%02d:%02d",hours,minutes,seconds];
    .......
}
4

2 回答 2

1

我按照 jabobadilla 的以下代码解决了这个问题

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

AppDelegate.h

@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-16T08:23:58.900 回答
0

您的 IBOutlet 可能有问题....

尝试创建一个程序化的 UILabel 并将_mainTimerLabel值传递给该标签....

这可能会帮助你..

于 2013-05-15T03:20:53.180 回答