Maintaining view controller data after switching to another view controller
I have a view controller maintaining laps in table view. If I move to another controller a开发者_JAVA技巧nd came back, I need to maintain the data. How can I do this?
I'd recommend moving the data out of the view controller. That way the controller can attach/detach to the value as many times as necessary (when it is loaded/unloaded) and the value will persist.
I dont quite understand why you're losing the data - I'd expect that your UIVieeController is staying in memory.
If however you want to extract the data out of the scope of the controller you could put it somewhere with a global scope. The UIApplicatuonDelegate can be accessed by all controllers in your apps object graph and could therefore hold the data.
To access your UIAppicationDelegate...
[[UIApplication sharedApplication] delegate]
You'll need to add a nee propety to your custom UIApplicationDelegate that will store the laps...
@property(atomic,retain) NSMutableDictionary lapTimes;
Now in your lap time UIViewController (and any other controller) you can get a reference to the property using...
MyAppDelegate* appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableDictionary lapTimes = [appDelegate lapTimes];
If you're looking to persist across multiple runs of the application and using UITableViews then it may also be worn investing some time in reading a bit about Core Data.
精彩评论