ios代理,多个界面代理问题。

本文探讨了在iOS应用中,如何处理多个界面作为同一代理的情况。通过示例代码展示了在GCTwoViewController中,ViewController1和ViewController2同时作为其代理,关键在于每个界面设置代理时传入的`self`是唯一的。代理可以被多个类实例使用,但每个`self`对应一个唯一的代理,确保了调用的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>

@protocol TwoViewDelegate <NSObject>

- (void)changeValue1;
- (void)changeValue2;

@end
@interface GCTwoViewController : UIViewController


@property (nonatomic,unsafe_unretained) id<TwoViewDelegate>delegate;
@end




在  GCTwoViewController.m文件中  

 [self.delegate changeValue1];

  [self.delegate changeValue2];


在界面ViewController1和ViewController2同时成为GCTwoViewController的代理


ViewController1  中

 GCTwoViewController.delegate = self


在ViewController2中

 GCTwoViewController.delegate = self

ViewController1和ViewController2中的changeValue1方法都可以用

是因为注册代理的时候传了self给代理 ,代理知道被谁调,还是一对一的关系。所以没问题


代理可以被多个界面调用,但是保证注册代理传入的self是唯一的。


使用细节

delegate     [self.delegate updateAlert];


在其他使用的界面中       timer.delegate = self; //设置代理实例  


delete可以被多个类代理  但是进来的时候是一一对应的 就可以了 


就是在一个self下只有一个delegate



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值