Objective C: Multiple delegates - iphone

I am curious if and how to make a Controller be the delegate for two different objects.
Is this allowed or is this like multiple inheritance in Java?
Suppose I wanted to have one controller that responded to: <UIAccelerometerDelegate> and <CLLocationManagerDelegate>
Would the header file look like this?
#interface MainViewController : UIViewController <UIAccelerometerDelegate> AND <CLLocationManagerDelegate> {

Actually, it works quite well. Declare your interface like this:
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate>
and then implement the methods from both delegate interfaces.

Nope, like this:
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate> {

#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate>

As simple as that:
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate>

Related

Delegate AVAudioPlayer for audioPlayerEndInterruption method

My ViewController name is Mainviewcontroller in which i m doing all the actions of AVAudioplayer.
If in h file i do
#class MainViewController;
#protocol MainViewControllerDelegate
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
#end
#interface MainViewController : UIViewController <UIActionSheetDelegate, InfoDelegate, AVAudioPlayerDelegate>
I am confused in the above statement i m supposed to do AVAudioPlayerDelegate or MainViewControllerDelegate
If i do #protocolAVAudioPlayerDelegate then i get warning in yellow stating that duplicate protocol definition of AVAudioPlayer is ignored
when i have not defined #protocolAVAudioPlayer anywhere else.
Another confusion i have is that i m supposed to declare these methods in h file first only then implement in m file
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
Please help.
I think you are mis-using the #protocol construct here. You can go ahead and drop the entire #protocol block and just implement your class. You simply need to indcate your class is a delegate for an instance of AVAudioPlayer like this:
#interface MainViewController : UIViewController <UIActionSheetDelegate, InfoDelegate, AVAudioPlayerDelegate>
Then when you instantiate your AVAudioPLayer object, be sure to assign the MainViewController as the delegate:
audioPlayInstance.delegate = self;
Lastly, implement the two methods you want to be called on your AVAudioPlayer instance (I called it audioPlayInstance above). You only need to put these in your .m file, NOT you header. The header declaration for these methods is handled by the AVAudioPlayer.h class file.
Good Luck!

How to put more than one subclass in objective c

How can I do something like this ?
#interface SomeClass:NSViewController **:NSTableViewController** #end
How can i put two subclases in my class ??
Objective-C does not support Multiple Inheritance.
Typically, you work around this by using protocols when you want to program to an interface.
#interface SomeClass : NSViewController < SomeProtocol >
#end
Another option is composition:
#interface SomeClass : NSObject
{
#private
NSViewController * viewController;
NSTableViewController * tableViewController;
}
#end

Making a class a delegate of more than 1 thing?

I have 2 parts of my class that want it to be the delegate.
So I have:
#interface RewriteViewController : UIViewController <MPMediaPickerControllerDelegate>
And this causes an error with it saying it needs to be SKVocalizerDelegate.
And if i have:
#interface RewriteViewController : UIViewController <SKVocalizerDelegate>
It says the same about the MPMediaPickerControllerDelegate.
But putting:
#interface RewriteViewController : UIViewController <MPMediaPickerControllerDelegate> <SKVocalizerDelegate>
gives me a huge amount of errors. How can I make it the delegate for both?
You were almost there! You use a comma to show your class supports multiple delegates, like so:
#interface RewriteViewController : UIViewController <MPMediaPickerControllerDelegate, SKVocalizerDelegate>

Objective-C design pattern question about delegate

I have a class which implements many delegate methods. How to group the delegate methods into different classes by the protocol they belongs to and use them in the original class?
Rather than creating many classes, a simpler solution is to divide the class into different categories:
#interface MyViewController : UIViewController {
...
}
...
#end
#interface MyViewController (TableStuff) <UITableViewDataSource, UITableViewDelegate>
// methods related to table stuff
#end
#interface MyViewController (SearchStuff) <UISearchBarDelegate>
// methods related to table stuff
#end
Since categories just add methods to the existing class, you could use the any methods declared in a category in the "original" class.

classes in obj.c

#interface MainViewController : UIViewController < FlipsideViewControllerDelegate >{
}
I am quite new to object oriented languages & especially obj.c.
I need to use addressbook framework within this class.Apple documentation suggests this code:
#interface ViewController : UIViewController < ABPeoplePickerNavigationControllerDelegate > {}
I was wondering how to use at the same time FlipSideControllerDelegate & ABPeoplePickerControllerDelegate.
In fact I really can't understand what i am doing:)
You can declare your class as conforming to both the FlipsideViewControllerDelegate and the ABPeoplePickerNavigationControllerDelegate protocols like this:
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate,
ABPeoplePickerNavigationControllerDelegate> {}
Then implement the required methods in MainViewController's #implementation.