的UILabel不更新当返回的UIViewController(UILabel Not Updati

2019-09-02 01:50发布

我有了在的appDelegate一个NSTimer对象的简单应用到所有的意见进行访问。 该应用的结构与一个UINavigationController。 当我火的NSTimer对象,我的UILabel正在用正确的倒计时功能更新,但是当我回到RootViewController的和回倒计时来看,我的UILabel正在与当前倒数时间更新,但没有后续更新该的UILabel发生。 我在想什么? 我已经做了研究,确保该的UILabel对象不是零,我呼吁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];

}

Answer 1:

感谢大家的意见。 我实际上执行的是会去取回了的NSTimer正在更新我的AppDelegate,因为烧制的NSTimer方法是在主线程不再当我离开的看法,回到它的价值的方法来解决它。 只要我的NSTimer是有效的,此方法将循环。 我还放置一个延迟,允许UI更新值,然后再次执行该方法。 下面是在情况下,它可以帮助别人运行到类似的问题的代码。 我从搽旦提供的建议这样的想法,谢谢!

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

}


Answer 2:

类型转换是这样的:

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

实际上不会使UIApplicationDelegate到您的AppDelegate类并添加额外的参数。 因此将不会有指针在此变量定时器。

你需要存储的计时器指针不同的方法。



Answer 3:

尝试在主线程上的UILabel更新文本。 有时候,在更新用不的UILabel工作backgound线程。

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

如果您的appdelegate对象是工作的罚款和的UILabel正在与当前倒数时间更新,但没有后续更新到的UILabel结果,那么就喜欢它的主线程应用用户界面的变化

- (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更新用工作正常。 请让我知道,如果有任何问题仍然发生。



文章来源: UILabel Not Updating When Returning to UIViewController