classes in obj.c - iphone

#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.

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

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: 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.

How can one inherit two classes at same layer?

I am new at the area of iPhone. i am trying to build an iPhone app by using Cocos2d. I have used this type of classe like bellow-
#interface MenuScene : Scene {}
#end
#interface FlipView : UIImageView
{
CGPoint startTouchPosition;
NSString *dirString;
UIImageView *firstPieceView;
UIImageView *secondPieceView;
}
#end
#interface HelloController : UIViewController
#end
#interface MenuLayer: Layer{
Menu * menu;
NSString *dirString;
CGPoint startTouchPosition;
}
-(void) button1: (id)sender;
-(void) button2: (id)sender;
-(void) black_jack: (id)sender;
#end
and i want to inherit two classes(FlipView, HelloController ) to MenuLayerClass. but how can i do it. Actually what will be syntax. Pls reply any comment with code or syntax how i can do it.
You can't. As Clark says, Objective-C does not support multiple inherritance. This is because the designers believe that the advantages of multiple inherritance do not justify the complexity and bad design it encourages.
Instead, they have included something that will meet your needs. You can declare a 'protocol' using the #protocol directive. A protocol describes a set of methods a class responds to but cannot add data to an object.
To use a protocol, you include the protocol name in angle brackets after the super class.
e.g.
#protocol myProtocol
-(void)myProtocolMethod
#end
#interface myClass : NSObject <myProtocol>
{
int someData;
}
Will give an NSObject subclass that must also respond to (void)myProtocolMethod messages.
That said, I would agree with Clark that you should review your design - having a single object that is both FlipView, HelloController does not sound good. You should probably implement a FlipController and use a third class (the model) to synchronise state between the two controllers - or if your app is very simple, have a single class that acts as a delegate for both FlipView and UIController.
You cannot, as Objective-C does not have multiple inheritance. Additionally, it doesn't really make sense to have a single class be both a view and a view controller.