返回UIViewController时UILabel不更新 [英] UILabel Not Updating When Returning to UIViewController

查看:76
本文介绍了返回UIViewController时UILabel不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple app that has an NSTimer object in the appDelegate to be accessed by all views. The structure of the app is with a UINavigationController. When I fire the NSTimer object, my UILabel is being updated with the correct countdown function, but when I go back to the rootViewController and back to the countdown timer view, my UILabel is being updated with the current countdown time, but no subsequent updates to the UILabel happen. What am I missing? I have done research on making sure the UILabel object is not nil, that I call the function on the viewDidAppear method, and nothing seems to work! Here is the code:

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



AppDelegate.m



AppDelegate.m

@implementation AppDelegate

@synthesize countdownTimer;



CountdownTimerViewController.h



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



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

}


推荐答案

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

Thanks to everyone for your comments. I actually solved it by performing a method that will go and retrieve the value that the NSTimer is updating in my AppDelegate, since the method firing the NSTimer is no longer in the main thread when I leave the view and come back to it. This method will loop as long as my NSTimer is valid. I also placed a delay, allowing for the UI to update the value, and then perform the method again. Here is the code in case it helps someone running into a similar issue. I got this idea from the suggestion provided by chandan, thanks!!

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

}

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



AppDelegate.m



AppDelegate.m

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

}

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



CountdownTimerViewController.h



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



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

}

这篇关于返回UIViewController时UILabel不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆