View.UILabel.text 在 viewController 之间切换后不会更改



我用过NSTimer,在倒计时结束时想改变View.UILabel.text=@"Time Up"的值。这工作得很好,直到我切换到其他一些 ViewController,在 viewController 之间切换(并回到这个 viewController)后,当倒计时完成时,我得到了完美的值并且没有零,但 View.UILabel.text 值没有改变,我已经检查了异常,没有异常被引发。

我的代码:

-(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()中更改标签的值,它工作正常,但是我需要在倒计时完成后更改文本。我正在使用UINavigationController & MFSideMenuManager在视图控制器之间切换。

我的导航代码:

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

一种可能的解释是,您不是回到原来的视图控制器,而是回到一个新的实例。当使用标准 segue 而不是展开 segue 时,这很容易发生。

请参阅发生类似问题的另一个问题:更改方法内的标签文本

如果您通过单击后退导航按钮返回到视图,并在 viewDidLoad 上调用此方法,这就是问题所在。

viewDidLoad 仅在创建视图控制器后调用,要在每次打开视图控制器时调用这些方法,请在 viewWillShow 上调用这些方法。

最新更新