delegate & protocol simple code demo

来源:互联网 发布:java 获取项目名称 编辑:程序博客网 时间:2024/06/05 12:49

在Objective-c 中,不同对象间通信貌似只能通过protocol & delegate 实现。

sample code:

需要发送消息给其他类的类头文件中:

@protocol ShakeDelegate <nsobject>

@optional

//- (void)shakeAnimationStart;

- (void)callShakeAnimationStart;

- (void)callShakeAnimationStop;

@end

该类中添加变量:

id <shakedelegate> delegate;

M文件中发送信息:

[self.delegate callShakeAnimationStart];

在需要接受信息的类中:

@interface  添加继承

<ShakeDelegate>

设置需要传递的类的instance代理对象为自己:

childInstance.delegate = self;

在自己类实现delegate 方法:

- (void)callShakeAnimationStop {

   // oops , i received the message from other class  ....

  }

完成。

 

http://blogs.iteye.com/blog/417209

原创粉丝点击