I’m building an iPhone app that loads 5 items on a TabBarController.
When the app loads the first view is an audio player (an instance of MyAudioPlayerViewController)
Suppose the user starts to play a song on MyAudioPlayerViewController and then decides to tap on the second Tab Bar Item, which has 6 options on a UITableView to choose from. The user makes a selection, that leads to another UITableView (with some other options to choose from), then they make another selection, leading to a final ViewController, that is an audio player as well.
I need this final UIViewController to be the SAME as the first one that had the inicial song playing and NOT a new instance of MyAudioPlayerViewController.
Why do I need this configuration? I need it because in the very moment that the user makes his final selection on the previous UITableView the song that is playing MUST STOP.
This final view having the MyAudioPlayerViewController must be able the access my custom NSObject class that is playing the audio, making it stop the current song.
Right now I have this code that is invoking MyAudioPlayerViewController that shows the final UIViewController:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyAudioPlayerViewController * myAudioPlayerViewController = [[MyAudioPlayerViewController alloc] initWithNibName:@"MyAudioPlayerViewController" bundle:nil];
[self.navigationController pushViewController:myAudioPlayerViewController animated:YES];
[myAudioPlayerViewController changeText:[arraySingerList objectAtIndex:indexPath.row] withSong:[arraySongList objectAtIndex:indexPath.row]];
[myAudioPlayerViewController release];
}
As you can see it makes a new instance of MyAudioPlayerViewController, that has nothing to do with the very first one when app launched, then I’m able to invoke the method on my custom NSObject class (the player itself) responsible to stop the previous audio that is playing because it is protected.
Is there any solution for this case or should I build everything again using another approach?
Thanks!
You need to be able to reference the MyAudioPlayerViewController instance from your other classes. You can do this through your app delegate if you like.
If your tab bar controller is set up using Interface Builder, create an IBOutlet in your app delegate of class MyAudioPlayerViewController and hook it up to your MyAudioPlayerViewController in Interface Builder.
If you’re setting up the tab bar controller in code, simply alloc/initialize the instance variable and set it as your first tab.
You’ll need to either set its properties or create an accessor method so you can access it from other classes.
To set up the property, open up your app delegate .h file and add this:
and in your app delegate .m file add this:
Then:
For more info about properties in Objective-C: http://cocoacast.com/?q=node/103