Set delegate in a different class
I'm trying to write my first iPhone app and i'm having some trouble with a delegate.
I have a class that utilises the AVAudioPlayer to play some sound, I then have another class that I need to do something when that sound is complete. So I need this class to implement audioPlayerDidFinishPlaying method of the audio player.
The problem is, I can't work out how to make my second class a delegate of th开发者_如何学Ce audio player that is a member of the first class. I created a method in the first class that takes a reference to a class and runs the setDelegate method on this, but the audioPlayerDidFinishPlaying method never gets called. However if I implement the audioPlayerDidFinishPlaying method in the first class, it gets called fine. How can I get audioPlayerDidFinishPlaying to be called in the second class?
So, the code I'm using to add the delegate, in the first class I have this method:
-(void) setAudioDelegate:(id)delegate{
[audioPlayer setDelegate:delegate];
}
then in the second class i'm calling it from this
[class1 setAudioDelegate:self];
Confirm that both 'audioPlayer' and 'class1' are not nil at the time you attempt to set the audio delegate.
The delegate works in one-to-one manner. If you want relations between instances works in many-to-many or one-to-many manner, maybe you could study NSNotification class reference, or Notification Programming Guide.
精彩评论