2014-12-03 24 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上调用这些方法。