views:

234

answers:

2

Hi, 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 inherite 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.

+1  A: 

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.

clarkcox3
Exactly, focus is on using delegates and not on subclassing
epatel
+5  A: 

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.

Roger Nolan