您的位置:首页 > 移动开发 > Objective-C

objective-处委托协议

2012-05-19 18:30 447 查看
满足正式协议 protocol 的委托只能执行协议中的方法,

#import <Foundation/Foundation.h>@protocol GameBoardControllerDelegate <NSObject>
@optional
- (NSInteger)getColumns;
- (NSInteger)getRows;
@end


#import "cocos2d.h"
#import "GameBoardControllerDelegate.h"    //MVC中的C
@class GameBoard;
@class GameBoardView;

@interface GameBoardController : CCNode <GameBoardControllerDelegate>
{
GameBoardView *_view;
}
@property(nonatomic,retain) GameBoard *gameBoard;    //MVC中的M
@property(nonatomic,retain) GameBoardView *view;   //MVC中的V

- (NSInteger)getColumns;
- (NSInteger)getRows;
@end


#import "cocos2d.h"
#import "GameBoardControllerDelegate.h"
@interface GameBoardView : CCNode
{
id  <GameBoardControllerDelegate>_delegate;     //满足正式协议的传过来的  GameBoardController指针

} @property(nonatomic,assign)id <GameBoardControllerDelegate> delegate; - (void)initView; @end


#import "GameBoardView.h"
@implementation GameBoardView
@synthesize delegate = _delegate;

- (void)initView
{
for (int i = 0; i <[_delegate getRows]; i++) {
for (int j = 0; j< [_delegate getColumns] ; j++) {
// position and render game board spaces
}
}
}

@end


注:如果 delegate 不满足协议,直接传递 GameBoardController指针

则delegate 可以执行 GameBoardController 中的一切方法,也可以调用其的属性变量
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: