How do I share an object between UIViewControllers on iPhone?

前端 未结 5 728
清酒与你
清酒与你 2020-11-28 22:48

My application is a tab bar application, with a separate view controller for each tab.

I have an object in my first view controller (A) which contains all my stored

5条回答
  •  清酒与你
    2020-11-28 23:00

    I like to create a top level Model class that is a singleton and contains all the elements I might need.

    It's helpful to also give it a top level load method that populates objects with just the db keys, using the hydrate/dehydrate pattern common in the Apple examples.

    Typical usage in the app delegate would be simply,

    [[MyModel sharedModel] load];
    

    And then in a view controller:

    NSArray *myThing1s = [[MyModel sharedModel] thing1s];
    NSArray *myThing2s = [[MyModel sharedModel] thing2s];
    

    You can then iterate over your thing1s and thing2s and when you need details, you can just call

    [myThing1 hydrate];
    

    which will populate the object.

    Of course, you probably want to use CoreData to manage the persistence from 3.0 onwards.

提交回复
热议问题