MouseCrasher
MouseCrasher

Reputation: 473

Static View For Every View Controller in iOS

I am creating view controller with a view. The view is named as timerView and it contains a timerLabel. Label is being updated by NSTimer after every second. Its working fine.

But what I want to do is, whenever any other view controller appears, timerView should be available on it and it should also being updated with NSTimer regularly.

I have a solution to create a timerView, timerLabel and NSTimer for every view controller. Then I have to update it by last view contorller timers value. But its not feasible.

How can I create only a single view and use it on every controller with regular update of single second.

Any suggestion will be appreciated.

Upvotes: 2

Views: 145

Answers (2)

HarshIT
HarshIT

Reputation: 4915

You may also add a view controller's view and similarly a view. Kindly visit following url. It may be useful.

iPhone how to add a view controller's view to another view controller's view?

Upvotes: 1

Caleb
Caleb

Reputation: 125037

I just want to create only a single view and want to use it on every controller with regular update of single second.

Why? Instead of using a single timer view, create as many as you want and use a single timer to drive them. Removing the timer view from one view as it disappears and adding it to another that's about to appear is possible, but I don't think it really gets you anything useful.

If the current elapsed or remaining time is important for the whole app, you should have a single source for that information. That is, you should have a single timer, and use that to drive whatever timer views are being displayed. An easy way to do that would be to have the timer's action post a notification, and have any timer views that are visible listen for that notification. This minimizes coupling between the timer and any timer views -- the timer doesn't need to know about the timer views, and the timer views don't need to access the timer directly.

Be sure to have the timer view's start listening for the notification when they appear (so that they begin updating) and stop listening when they disappear (because they don't need to update if they're not visible).

Upvotes: 2

Related Questions