I’m new to xcode and I’m struggling with this for 5 days now, my head is almost blowing away because I know this must be pretty simple!
I have a tab bar based iphone app with 2 tabs.
On the first one I want to create new Items and store them in a NSMutableArray inside a “Repository” NSObject Class that I’ve already created.
This Repository class has methods like add, remove, update and fetchAll Items. The last one is the method that returns all the items stored in the NSMUtable Array.
There’s also a NSObject “Item” Class that represents what is an Item.
On the second tab I have a table view controller that I want to fill up with the items created on the first tab and that have been stored in the “Repository” Class.
My question is:
- I have to alloc init my repository somewhere in the process so that I can call the “Repository” class methods. The thing is, if I alloc init the repository on the first tab controller, everytime that the first tab is loaded it will alloc init the repository again and the items that where there just disappear. The same goes for the second tab.
So where should I alloc init my repository so that I can call the “add” method in the first tab view and call the “delete” and “fetchAll” methods in the second tab view (that is a table view) and guarantee that I’m acting on the same repository data?
Thanks in advance
It seems that you might need a singleton.
A singleton is a unique global object that you can access from anywhere in your app. On the first access, it will be also created. On any subsequent access, the existing object will be returned.
Have a look at this post for a good tutorial.