Syntax for creating a view delegate in Objective-C

I am trying to create a delegate protocol for a custom UIView. Here is my first attempt:

@protocol FunViewDelegate
@optional
- (void) funViewDidInitialize:(FunView *)funView;
@end


@interface FunView : UIView {    
@private
}

@property(nonatomic, assign) id<FunViewDelegate> delegate;

@end

This does not work because the FunView interface was not declared during the declaration of FunViewDelegate. I tried adding a prototype ala C ++ before @protocol:

@interface FunView;

But it just drives the compiler nuts. How am I supposed to do this?

+3
source share
2 answers

Direct class syntax @class Foo;, not @interface Foo;.

+10
source

It would seem that you can redirect ad protocols:

@protocol FunViewDelegate;

@interface FunView : UIView {    
@private
    id<FunViewDelegate> delegate;
}
@property(nonatomic, assign) id<FunViewDelegate> delegate;
@end

@protocol FunViewDelegate
@optional
- (void) funViewDidInitialize:(FunView *)funView;
@end
+9
source

Source: https://habr.com/ru/post/1699580/


All Articles