-1

I have used NSTimer & at the end of countDown want to change the value of View.UILabel.text=@"Time Up". This is working perfectly until I switch to some other ViewController, after switching between viewController(& coming back to this viewController) when countDown is complete I am getting perfect values & their is no nil but View.UILabel.text value is not changing, I have checked exceptions, no exception get raised.

My Code:

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

}

Please any help will be very appreciated. (I have seen many questions but my issue is not solved)

*Note: After switch if i change the value of label in ViewWillAppear() it is working perfect, but i need to change text when countDown is completed. I am using UINavigationController & MFSideMenuManager to switch between ViewControllers.

My Navigation Code:

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

}

2 Answers2

1

One possible explanation is that you are not coming back to the original view controller, but to a fresh new instance. This can easily happen when using a standard segue instead of an unwind segue.

See this other question where a similar problem occurred: Changing label text inside method

Community
  • 1
  • 1
Clafou
  • 15,250
  • 7
  • 58
  • 89
  • i am not using Segue, instead doing every thing programmatically – Syed Nazar Muhammad Dec 03 '14 at 11:47
  • Can you put a breakpoint in your "Time Up" code block and see if it gets called at all? If it does but you don't see the result displayed, I think it may be that you have multiple instances. Try a breakpoint in your View Controller's initializer and see if it gets executed more than once. – Clafou Dec 03 '14 at 11:50
  • Yeah I have checked it by putting breakpoints "Time Up" block is being called – Syed Nazar Muhammad Dec 03 '14 at 11:52
  • And does your view controller's initializer get called more than once? – Clafou Dec 03 '14 at 11:56
  • MyProfile is a view which takes the whole view of ViewController by self.vew = profileView; & I have initilize the view in -(void)loadview like profileView = [[MyProfile alloc] ininiwitht..]; – Syed Nazar Muhammad Dec 03 '14 at 11:59
  • No I have my initializers in loadView & its just called once per switch – Syed Nazar Muhammad Dec 03 '14 at 12:00
  • Even if you do it programmatically, it could be collected by the garbage collection.. Best you could do is to see if the VC has strong references to it. If it does you 'should' be able to rule out that it will load a new instance. Cause im afraid everything points to this. – Ńike Kamstra Dec 03 '14 at 12:02
  • "it's just called once per switch"... What do you mean by switch? In case you mean it gets called again when you return from the second view controller, then I guess that's the problem. – Clafou Dec 03 '14 at 12:08
  • Yes I am very new to Objective c programming, yes when i return back to view controller my loadview is called every time & i guess you are right this might be the issue, & in this loadview I am initializing MYProfile view which is a subview of viewController. What will you recommand me to do. please – Syed Nazar Muhammad Dec 03 '14 at 12:12
  • Hmm, I have something similar running on my app.. I also switch programmatically and it's not showing up on viewDidLoad (as it should). viewDidLoad is called once to instantiate, and every time the view controller needs to appear, it only calls viewDidAppear or view WillAppear. So yes, it is almost surely making a new instance.. Could you provide the bit of code that allows you to switch between view controllers programmatically? – Ńike Kamstra Dec 03 '14 at 12:17
  • I don't know about MFSideMenuManager but perhaps it is designed to create a new view controller rather than reuse existing ones. One easy way to bypass your problem entirely would be to use NSNotificationCenter instead of a direct reference to your view. When your timer ticks (or completes, to minimise notifications), just fire a notification and do nothing else. Make sure that your ProfileView registers for this notification and reacts accordingly by displaying "Time Up". – Clafou Dec 03 '14 at 12:20
  • @Clafou I have done this too, I did try using NSNotificationCenter this when my timer's countDown complete, & fire a notification & in that notification I did try changing text of label, that too work until I am on the same viewController but when I switch between viewControllers the notification is fired but the value of label is not changed without any exception – Syed Nazar Muhammad Dec 03 '14 at 12:27
  • Did you register for the notification each time a profile view is created? The best place to register is inside the init method of your profile view class (MyProfile, judging from your previous comment), this makes sure all instances are registered. – Clafou Dec 03 '14 at 12:32
  • Your this comment got big on me – Syed Nazar Muhammad Dec 03 '14 at 12:39
  • By the way, if you go this route remember to unregister from the notification center. – Clafou Dec 03 '14 at 13:45
1

If you are returning to the view by clicking on the back navigation button, and calling this methods on viewDidLoad that is the problem.

The viewDidLoad are only called once the view controller is created, to call those methods every time when you view controller opens, call those methods on viewWillAppear.