2014-12-03 74 views
-1

我在countDown的末尾使用了NSTimer &想要更改[email protected]"Time Up"的值。這是完美的工作,直到我切換到其他ViewController之後切換之間切換viewController(&回來這個viewController)時,countDown完成我越來越完美的價值&他們是沒有零,但View.UILabel.text值沒有改變,我已經檢查異常,沒有發生異常。view.UILabel.text在viewControllers之間切換後不改變

我的代碼:

-(void) timeInterval 
{ 
appdelegate._UserProfile.totalTime = 30; 

UIApplication *app = [UIApplication sharedApplication]; 

//will allow NSTimer to continue running in background.. 

UIBackgroundTaskIdentifier bgTask = 0; 

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 

[app endBackgroundTask:bgTask]; 
    }]; 

appdelegate._UserProfile.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(countDown) userInfo:nil repeats:YES]; 

} 

-(void) countDown 
{ 
    appdelegate._UserProfile.totalTime -= 1; 

if(appdelegate._UserProfile.totalTime == 0) 
{ 
    [profileView.userPhoneNo setText:@""]; 

    profileView.timeUp.text= @"* Time Up"; 
    } 

} 

請任何幫助將是非常讚賞。 (我看到很多問題,但我的問題沒有解決)

*注意:切換後,如果我更改ViewWillAppear()中的標籤的值它工作完美,但我需要改變countDown完成時的文本。我正在使用UINavigationController & MFSideMenuManager在ViewControllers之間切換。

我的導航代碼:

-(void) initMyProfile 
{ 
UINavigationController *detailNavigationController = [MFSideMenuManager sharedManager].navigationController; 

detailNavigationController.menuState = MFSideMenuStateHidden; 

MyProfileViewController_iPhone* detailViewController = [[MyProfileViewController_iPhone alloc] initWithNibName:nil bundle:nil]; 

    detailViewController.title = @"My Profile"; 

detailNavigationController.viewControllers = [NSArray arrayWithObject:detailViewController]; 

} 

回答

1

一個可能的解釋是,你不回來原來的視圖控制器,而是一個全新的實例。當使用標準的賽格而不是放鬆的賽格時,這很容易發生。

見發生了類似問題,在此的其他問題:Changing label text inside method

+0

我沒有使用Segue,而是以編程方式做每件事 – 2014-12-03 11:47:12

+0

你可以在你的「Time Up」代碼塊中放置一個斷點,看它是否被調用?如果它確實存在,但你沒有看到顯示的結果,我想可能是你有多個實例。在你的View Controller的初始化器中嘗試一個斷點,看它是否被多次執行。 – Clafou 2014-12-03 11:50:49

+0

是的,我已經通過放置斷點來檢查它是否正在調用「Time Up」塊 – 2014-12-03 11:52:24

1

如果要返回到視圖通過單擊後退導航按鈕,並呼籲viewDidLoad中這種方法這就是問題所在。

只有在創建視圖控制器時才調用viewDidLoad,每當您查看控制器打開時調用這些方法,在viewWillAppear上調用這些方法。