Instances of Objects in Objective-C - iPhone/iPad
I am developing an iOS app that is 开发者_JAVA百科a calculator to keep track of a score of a certain game. The game has many view controllers. I am going to create a "Player" object and create 4 instances of that player. Now in terms of Objective-C, how would I keep those instances alive between several view controllers? Should I for example, create an array of players and keep passing the array from one VC to another as the view progresses?
Thank you,
using singleton class , you will get the anywhere the same object. you need not to create each and every time. it create once and you use the app whole.
You can create the 4 instances in AppDelegate.
Then these 4 instances will be accessible by all view controller using shared AppDelegate object.
Let me know in case of any difficulty.
Cheers.
Just a quick thought, Passing array of Players can resolve your problem. But quickest way to share objects between view controllers is through using Application Delegate as you can simply access application delegate anywhere.
you can use of ApplicationDelegate to store your progress. when you move to another view at that time update your ApplicationDelegate variable in - (void) viewWillDisappear:(BOOL)animated
method and at another view in viewLoad method get the updated value from the delegate variable.
精彩评论