How should I pass an array between view controllers?
I'm building an app that uses a TabBar controller in which one of the view controllers an NSMurableArray is set (and potentially modified). I then need to use that same array to populate a table on one of the other views when it is selected. I've looked into using a singleton (as was provided as an an开发者_运维问答swer to this question on this website already) but haven't had much luck.
Would a singleton be the best way to go or is there a better way of doing it? I'd appreciate any help/examples if possible?
You have several options for doing this, here are 2...
1.) have the NSMutableArray be a property of the one view controller so the other one can access it like viewController1.mutableArray
. To do just add @property (nonatomic, retain) NSMutableArray *mutableArray
to your viewController class (using whatever name you want).
2.) Pass the array through a method from the first viewController to the other and keep a reference to that array in the other class.
You can 'attach' controllers to each other, just like you 'attach' ui elements to controllers. E.g., declare variable
IBOutlet ReferencedController *referencedMenu;
and then in Interface Builder draw a line between referenced and referencing controllers.
Do I understand you correctly?
Thanks for your help, in the end this is how I have done it:
UIViewController *tmpNamesListVC = [self.tabBarController.viewControllers objectAtIndex:1]; self.names = [tmpNamesListVC names];
When it builds i'm getting a 'UIViewController may not respond to -names' warning but everything compiles and runs as it should.
精彩评论