\I have a view controller which starts a count down timer and updates a button label according to it.The problem is that when the user leaves the view and come back i do not want to restart the timer o continue from the paused time.What would be the best way to active it?
Making the timer in app delegate and post notification to the this view controller.Since the other view controllers do not need it they do not need to listen to any notification.The problem is that i have to make sure that the timer has run down to 0 to enable the button os it means when the view is loaded again i need to wait for the notification at least 1 second to make sure that he time is really up.The problem is what to show during this period on the button label.
If the view controller was a tab bar item i would be no problem but it is a pushedv view contller so it is released when the user taps on back.
preguntado el 31 de julio de 12 a las 12:07
Just create a singleton object for managing your timer(s). You can create it from appdelegate. You can get remaining time from that object anytime you want (firedate - datenow). So you do not need to work with notifications. It is also not good idea to send a notification in every second, which is not used by your view, when it is not allocated. When your view is allocated / comes to screen can ask that object every second the remaining time and updates his label or some subviews etc. You can implement it easily with a NSTimer object.
You can also place a bool variable in that singleton object, which indicates, if firedate reached. Or check remaining time < 1 . So your view check the value of the variable while is allocating/showing then you can decide to activate or deactivate your button.