views:

163

answers:

1

I have an array of booleans (soundArray) that is modified based on user input. Therefore it is owned by the main UIView that the user is interacting with. Meanwhile, in another separate controller (GestureController), I need access to this soundArray, so I can pass it as a parameter in a method call.

I'm pretty sure a ViewController shouldn't be digging out properties of a UIView it doesn't own (correct MVC), so I've created another pointer to the soundArray in the ViewController (MusicGridController) that owns the main UIView (MusicGridView).

In GestureController, I have the following code:

 // instantiate the sound thread
 NSArray *soundArrayPointers = [NSArray arrayWithObjects:self.musicGridViewController.soundArray, nil];
 [NSThread detachNewThreadSelector:@selector(metronome:) toTarget:[MusicThread class] withObject:soundArrayPointers];

(eventually I'll have more soundArrays in that array).

Now, in MusicGridController, I need to set the pointer to the SoundArray in the UIView MusicGridView... so I did this:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(20, 0, 480, 480);
    MusicGridView *tempview = self.view;  // this line gives me a warning that I am initializing from a distinct Objective-C type, which I don't understand.

    self.soundArray = tempview.soundArray;
}

I tried self.soundArray = self.view.soundArray, but it threw an error, and I don't understand why. So my question is: Is this a correct implementation? How do I get from one viewController to a property of a given UIView?

+1  A: 

I'm not sure I understand who the NSArray belongs to, or who it should belong to, but you can try utilizing the AppDelegate if it's supposed to be a global object (accessed by more than one view and/or controller).

Now, to solve your problem. Your implementation is not wrong, and the warning will go away if you add (MusicGridView *) before self.view. This is related to the error you got when you tried self.soundArray = self.view.soundArray, and the reason is simple: UIView doesn't have a soundArray property, and self.view is a pointer to a UIView. By casting self.view to a MusicGridView *, you get rid of the warning, and your code should work fine.

Can Berk Güder