Question

I have no idea if I'm using the right terminology here, but I'm hoping someone can help - I'm pretty new to iOS Dev and have run into a problem:

I have created a custom class that is used to store an object. It has a bunch of properties and functions, just as an object should. This object is being declared in my ViewController's .h file, and initialised and used throughout the ViewController. The object holds a bunch of information about a test, and takes some measurements in various threads.

The problem I'm facing is that when I load another view (using ECSlidingViewController for the menu) and then return to the view which had the object...it seems to have forgotten the object. The tasks running in the threads are all still running, but that instance of the object seems to be gone.

Is there a way to preserve an instance of the object when changing views so that when I return to the appropriate view, the object is still there and I can still use it?

Thanks!

Was it helpful?

Solution 2

Turns out that my menuViewController that was implementing the ECSlidingViewController was recreating the view that contained the object each time I selected the item from the menu. Thanks @rdelmar for pointing this out!

The object was persisting, but in a viewController that was being replaced each time it was selected from the menu and hence the object was unreachable.

I simply implemented this and references to the viewControllers are now being stored in a mutable dictionary and simply recalled when the view is needed, rather than recreating the view. This means that the viewController is being reused and the object is persisting with it.

OTHER TIPS

I'd recommend you to put the objects reference in a transversal class, something like a manager or any other class which you consider appropriate according to your design and most important it needs to be a class that you're completely sure won't be release nor re-created as view controllers are usually when you change from one view controller to the other. A singleton instance that manages your main logic could be a good option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top