Rob
Rob

Reputation: 4239

Creating Delegate methods in a Protocol

I cannot seem to build my protocol the way I would like and I have narrowed down to a problem with using derived classes. If I use a cocoa class it seems to work. Here is what I have...

#import <Foundation/Foundation.h>
#import "MyView.h"

@protocol MyDelegate  
- (void)view:(MyView *)aView didDoSomethingWithString:(NSString *)string;
@end

The MyView class is...

#import <UIKit/UIKit.h>
@interface MyView : UIView {  
    NSString *whatever;  
}  
- (void)myMethod;  
@end  

@implementation MyView  
- (void)myMethod {  
 doSomething...  
}  
@end

So when I attempt to build I get the error "Expected ')' before 'MyView'". If I replace the custom class MyView with UIView then the code compiles. I am hoping someone sees something that I am overlooking. Any ideas are appreciated.

Thanks.

Upvotes: 0

Views: 562

Answers (2)

Ned
Ned

Reputation: 6270

Try putting the @interface and @implementation parts in different files (if you currently have them in the same file). It looks like you have all that in MyView.m, and you're importing MyView.h, which doesn't exist.

Upvotes: 1

seppo0010
seppo0010

Reputation: 15889

Are you sure MyView.h contains @interface MyView : UIView?

Also, instead of importing you can use @class. e.g.

@class MyView;
@protocol MyDelegate
- (void)view:(MyView *)aView didDoSomethingWithString:(NSString *)string;
@end

Upvotes: 2

Related Questions