0

I am building an application where by I want to share a mutable NSArray between two different view controllers.

The reason why, is because on one of the viewcontrollers (main) I need to actively check the contents of the array and if certain properties are satisfied show an alert, whilst the other viewcontroller (UI) is the UI for the user to set the an alert object into said array and manage the alert objets in a tableview.

So my question is where can this array reside, in my application delegate potentially?

Or am I best having it reside in the main (root view controller) and basically pass it to the second UI view controller and dynamically populate the UItableview there each time, then pass it back to update the array on the main?

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • 1
    Maybe take a look at the delegate design pattern, you should be able to craft it to your case. http://enroyed.com/ios/delegation-pattern-in-objective-c-and-writing-custom-delegates/ – Oliver Atkinson Aug 04 '13 at 17:49
  • 1
    Are the properties specific to the state of the view or model? If it's the model then the data should reside in model classes. If it's the view then perhaps a main view controller would work that could be updated via the delegate pattern. – johnnieb Aug 04 '13 at 17:50
  • @OliverAtkinson, thanks for the comment, Yes I am familiar with delegate, but this wouldn't this paradigm basically be an array residing in one of the viewcontrollers just passed back and forth? – Woodstock Aug 04 '13 at 17:56

1 Answers1

9

There is many ways of doing that. It dependes on the hierarchy of your viewControllers and of your problem.

You could:

Etc.


Community
  • 1
  • 1
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • 1
    +1 for the big list of alternatives. Insightful. I'm glad you didn't include AppDelegate, Sqlite or NSUserDefaults – Rob van der Veer Aug 04 '13 at 18:13
  • 2
    Yeah, I first thought it would be worth at least mention it, but then I realized it was worthless. If someone else remember another valid ways, comment here and I'll edit the answer – Lucas Eduardo Aug 04 '13 at 18:16
  • @LucasEduardo I'm a little confused now! You know between the two viewControllers I have set up a delegate already (for properly dismissing the modally presented UI viewcontroller) so there I was ready to use the delegate protocol I already had created to pass my array back and forth when it dawned on me.... I can just pass the Array in as a parameter of the UI viewcontrollers init method! Then any changes done are instantly reflected as I passed the array as a pointer, I don't even need to pass it back using the delegate, am I missing something?? – Woodstock Aug 04 '13 at 18:37
  • Yeah, that would be correct. As you are passing a pointer, they will point to the same position of memory, and any change in one pointer will be reflected by the another one. My answer was kind of generic in pass data back and forth, not focusing in your problem. – Lucas Eduardo Aug 04 '13 at 18:48
  • @LucasEduardo Thanks for the info Lucas! So when would you use a delegate to pass back and forth rather than pass in as a pointer? And do you know how I can pass in the NSMutableArray as a "value" instead? (so then I can pass back a modified NSMutableArray and overwrite the other one) – Woodstock Aug 04 '13 at 19:10
  • 1
    Well, the delegate can be used in this case, when you pass forth a copy of an object and sometimes you want the changed value back and sometimes not, so you pass a copy instead of the same reference, otherwise the object will always be changed. And in objective C there's no direct way of passing an object by 'value', you would have to pass a copy of it, like `[myArray copy]` – Lucas Eduardo Aug 04 '13 at 19:30