ios中@protocol和@delegate

8 篇文章 0 订阅

    @property协议:

    协议就是规定了能干什么事

    delegate代理:

    代理就是能干协议里面规定的事的人

    通俗来讲,比如:我是个教师,主要工作负责上课,布置作业,评改作业,点名。由于事情多,时间太紧,这时候我需要一个助理(delegate)完成以下事情(protocol),评改作业和点名。

协议和代理的作用:

1.传值

2.类A中有类B,类 B中想要调类A中的方法

3.传事件

4.回调


//定义协议

@protocol PersonalProfileDelegate <NSObject>

- (void)getUserImage: (UIImage *)image;

@end

//委托类

PersonalProfileViewController.h文件

@interface PersonalProfileViewController : UIViewController 

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

PersonalProfileViewController.m文件

 if (self.delegate) {

        [self.delegate getUserImage: myView.userImageRight.image];

    }

//代理类

PersonalProfileViewController *personalProfile = [[PersonalProfileViewController alloc] init];

personalProfile.delegate = self;

- (void)getUserImage:(UIImage *)image {

    changeImage = image;

}


//@optional  //可选实现

//@required  //必须实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值