Reputation: 1031
I have a Tab bar app with 3 view controller, the FirstViewController have an audio player with the list of song and play/pause button. Now I want for SecondviewController and ThirdViewController to add a button who will call the method - (void) playpause of the FirstViewController class.
Looks easy but I didn´t find the way to make it working, i tried that :
- (IBAction) playpause1
{
PlayerAppDelegate *appDelegate = (PlayerAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate performSelector:@selector(playpause)];
NSLog(@"playpause test");
}
But this code only control the method in app delegate, not directly on the FirstViewController class like I need.
Any tips ?
Upvotes: 0
Views: 1440
Reputation: 14834
If I understood your question correctly... You can use optional protocol and delegate to achieve this. For example, in your SecondViewController's .h file:
@protocol SecondViewControllerDelegate -(void)playpause1; @end
@property (assign) id delegate;
In your FirstViewController's .h file, include this is the interface line:
The FirstViewController shold implement the -(void)playpause1 { NSLog(@"playpause test"); }
Upvotes: 0
Reputation: 124997
There are a number of possibilities:
Move the audio playing capability to a separate object, since it's a distinct task from what a view controller normally does. Then give all three view controllers a reference to that audio player and let them use it as necessary.
Have the object that creates the view controllers (typically the app delegate) give the second and third view controllers each a reference to the first view controller and let them send the -playpause
message.
Use notifications. The second and third view controllers can simply post a notification that the first controller will listen for. This avoids the need to have the three view controllers know about each other.
Upvotes: 1