2013-05-15 68 views
0

我一直有這個問題從過去幾個小時,並做了所有的搜索,我可以,但不幸的是,我沒有找到任何解決我的問題.... 情景:我有TimerViewController中的CountDownTimer ,NSTimer和其他方法在AppDelegate中設置,這是假設更新TimerViewController的Label ...按照標籤的setter,我得到的值正確,並顯示在NSLog中,但標籤沒有在屏幕上更新。 。此setter正在從AppDelegate中調用每次第二和標籤是假設給出的T,從appDelegate更新UILabel

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

回答

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

} 
0

在IBOutlet中可能有問題....

嘗試創建一個程序化UILabel並通過_mainTimerLabel v ALUE該標籤....

這可能會幫助你..