Reputation: 1965
I have a property declared like this in one of my view controllers:
@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
How can I access this property from other view controllers? Keep in mind that I'm using storyboards, not programatically loading views.
I'm a bit new to this..thanks.
Details
My root view controller has a TableViewController embedded within it which contains the property. Then I have another view controller that is pushed from the root view controller. When this view is pushed I want [musicPlayer play] to be called, but right now it doesn't recognize musicPlayer.
Upvotes: 0
Views: 1048
Reputation: 104082
You need to get a reference to the view controller with the musicPlayer property from your other controller. How you get that depends on how the two controllers are related. Is one presenting the other? Does one push to the other? Is one the child of the other? These different relationships require different way to access the other controller. You need to provide more information about your app structure for me to answer more fully.
After Edit:
Implement prepareForSegue so you can pass information to your presented view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
TriggerViewController *dvc = (TriggerViewController *)segue.destinationViewController;
dvc.tableViewReference = segue.sourceViewController.childViewControllers[0]
}
Here tableViewReference is a property in the controller where you're trying to access musicPlayer (tableViewReference should be of the class TriggerViewController). Then, in that class you can call play like this:
[tableViewReference.musicPlayer play];
Upvotes: 1
Reputation: 21966
Then you don't have to access the musicPlayer property from your root view controller, but from your table view controller. So it will be something like this:
[rootViewController.containerView.tableViewController.musicPlayer play];
Upvotes: 0
Reputation: 9170
Well one way is that you could #import the view controller into other view controllers and access the properties that way. For a storyboard you could use the controller's prepareForSegue:
method between the parent and child views. In the child view try something like childView *child = (childView *)segue.destinationViewController;
Upvotes: 1