0

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 answer 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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Nitrex, referring to your first suggestion which seems to make the most sense to me I'm not sure how I get the reference to the view controller from within the second view controller?
You can connect it as an IBOutlet in interface builder (plenty of tutorials out there on doing this) as mention in another answer.
0

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?

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.