从appDelegate更新UILabel

时间:2013-05-15 03:08:02

标签: iphone ios objective-c nstimer

我在过去几个小时里遇到过这个问题并且做了所有我可以进行的搜索但不幸的是,我找不到能解决我问题的任何事情.... 场景:我在TimerViewController中有一个CountDownTimer,NSTimer和其他方法在AppDelegate中设置,假设更新TimerViewController的Label ...按照标签的setter,我正确地获取值并在NSLog HOWEVER中显示标签没有在屏幕上更新......这个setter每秒都会从AppDelegate调用,而Label则假设显示Timer,

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

我已经仔细检查了标签,它正确地连接了界面,我试图用View String从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];
    .......
}

2 个答案:

答案 0 :(得分:1)

我按照以下代码通过jabobadilla

解决了这个问题

我实际上通过执行一个方法来解决它,它将检索NSTimer在我的AppDelegate中更新的值,因为当我离开视图并返回到它时,触发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];
    } 

}

答案 1 :(得分:0)

您的IBOutlet可能存在问题......

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

这可能对你有帮助..