Making a class a delegate of more than 1 thing? - iphone

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>

Related

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

Objective C: Multiple delegates

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>

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.

iphone - compiler conditional on header

I have a project that generates applications for two targets.
One of the targets has to include one additional delegate protocol that should not be present on the other one. So, I have created a macro on Xcode and declared the header like this:
#ifdef TARGET_1
#interface myViewController : UIViewController <UIScrollViewDelegate, UIPopoverControllerDelegate>
#endif
#ifdef TARGET_2
#interface myViewController : UIViewController <UIScrollViewDelegate>
#endif
{ .... bla bla.... }
The problem is that Xcode is hating this "double" declaration of #interface and is giving me all sort of errors. When I put just one of the declarations the errors vanish.
How to solve that? thanks for any help.
If you're getting a redecleration there you must have defined both symbols. Double check that your TARGET_1 and TARGET_2 defines aren't being defined together
I personally don't hesitate to write something like:
#interface myViewController : UIViewController <UIScrollViewDelegate
#ifdef TARGET_1
, UIPopoverControllerDelegate
#endif
>
It looks ugly, but I believe it better reflects the semantics.
You can even do one better:
#ifndef TARGET_1
#protocol UIPopoverControllerDelegate
#end
#endif
#interface myViewController : UIViewController <UIScrollViewDelegate, UIPopoverControllerDelegate>
All of this doesn't invalidate the previous answers of course!