2013-11-29 5 views
0

Я пытаюсь понять, почему тип не может быть прочитан из протокола. Я понял, что это потому, что @interface находится ниже протокола, но если кто-то может помочь мне разобраться в проблеме и объяснить мне, почему это было бы здорово.Делегат для iOS

#import <UIKit/UIKit.h> 

@protocol ARCHCounterWidgetDelegate <NSObject> 

    - (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget; 

@end 

@interface ARCHCounterWidget : UIView 

    @property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate; 

@end 

ответ

4

Вы должны либо forward declare класса или протокола:

// tell the compiler, that this class exists and is declared later: 
@class ARCHCounterWidget; 

// use it in this protocol 
@protocol ARCHCounterWidgetDelegate <NSObject> 
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget; 
@end 

// finally declare it 
@interface ARCHCounterWidget : UIView 
@property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate; 
@end 

или:

// tell the compiler, that this protocol exists and is declared later 
@protocol ARCHCounterWidgetDelegate; 

// now you can use it in your class interface 
@interface ARCHCounterWidget : UIView 
@property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate; 
@end 

// and declare it here 
@protocol ARCHCounterWidgetDelegate <NSObject> 
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget; 
@end 
+0

хаха прохладных Thats идеально. Большое вам спасибо за изучение цели-c. –

2

компилятор еще не знает о ARCHCounterWidget, и, следовательно, не может разрешить тип в методе делегата. Простое решение:

#import <UIKit/UIKit.h> 

// Reference the protocol 
@protocol ARCHCounterWidgetDelegate 

// Declare your class 
@interface ARCHCounterWidget : UIView 

    @property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate; 

@end 

// Declare the protocol 
@protocol ARCHCounterWidgetDelegate <NSObject> 

    - (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget; 

@end 

Он просто делает это так, что компилятор знает, что протокол определяется SOMEWHERE и можно ссылаться

Смежные вопросы