IOS委托机制

一、名词解释

protocol:协议,用来定义对象的属性,行为和用于回调,使用了这个协议后就要按照这个协议来办事,协议要求实现的方法就一定要实现。

协议中有两个关键字@private和@optional,@private表示使用这个协议必须要写的方法,@optional表示可选的方法,用不到可以不写。

delegate:委托,顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。

二、使用机制

当一个A view 里面包含了B view,而B view需要修改A view界面,那么这个时候就需要用到委托了。

使用委托的步骤:
1、在B view中定义一个协议和声明协议的方法:
  1. @protocol UIBViewDelegate <NSObject>  
  2. @optional  
  3. - (void)ontouch:(UIScrollView *)scrollView; //声明协议方法  
  4. @end 
2、在A view中实现协议的方法。
  1. - (void)ontouch:(UIScrollView *)scrollView  
  2. {  
  3. //这里实现协议  
  4. }  
3、在B view设置一个委托变
  1. @interface BView : UIScrollView<UIScrollViewDelegate>   
  2. {  
  3. id< UIBViewDelegate > _touchdelegate; //设置委托变量  
  4. }  
  5. @property(nonatomic,assign) id< UIBViewDelegate > _touchdelegate;   
  6. @end 
4、 把B view的委托变量设置成A view,意思就是 ,B view委托A view办事情。

  1. @interface AViewController : UIViewController <UIBViewDelegate> 
5、当B view中的 事件发生后,用委托变量调用A view中的协议方法

  1. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event  
  2. {  
  3. [super touchesBegan:touches withEvent:event];
  4. //方法检查
  5. if(_touchdelegate!=nil && [_touchdelegate respondsToSelector: @selector(ontouch:) ] == true)   
  6. [_touchdelegate ontouch:self]; //调用协议委托  
三、完整代码

  1. B_View.h:  
  2. @protocol UIBViewDelegate <NSObject>  
  3. @optional  
  4. - (void)ontouch:(UIScrollView *)scrollView; //声明协议方法  
  5. @end  
  6. @interface BView : UIScrollView<UIScrollViewDelegate>   
  7. {  
  8. id< UIBViewDelegate > _touchdelegate; //设置委托变量  
  9. }  
  10. @property(nonatomic,assign) id< UIBViewDelegate > _touchdelegate;   
  11. @end  
  1. B_View.mm:  
  2. @synthesize _touchdelegate;  
  3. - (id)initWithFrame:(CGRect)frame {  
  4. if (self = [super initWithFrame:frame]) {  
  5. // Initialization code  
  6. _touchdelegate=nil;  
  7. }  
  8. return self;  
  9. }  
  10. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event  
  11. {  
  12. [super touchesBegan:touches withEvent:event]; if(_touchdelegate!=nil && [_touchdelegate respondsToSelector: @selector(ontouch:) ] == true)   
  13. [_touchdelegate ontouch:self]; //调用协议委托  
  14. }  
  15. @end 
  1. A_View.h:  
  2. @interface AViewController : UIViewController <UIBViewDelegate>  
  3. {  
  4. BView *m_BView;  
  5. }  
  6. @end 
  1. A_View.mm:  
  2. - (void)viewWillAppear:(BOOL)animated  
  3. {  
  4. m_BView._touchdelegate = self; //设置委托  
  5. [self.view addSubview: m_BView]; 
  6. }  
  7. - (void)ontouch:(UIScrollView *)scrollView  
  8. {  
  9. //实现协议 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中绑定Unity的委托需要使用Unity提供的UnityBinding文件,该文件定义了Unity中的所有公共API,并提供了Objective-C接口,可以在iOS代码中使用。以下是一个简单的示例代码,演示如何在iOS中绑定Unity的委托: 首先,在Unity中创建一个C#脚本,用于定义需要绑定的委托方法。例如: ``` using UnityEngine; public class DelegateExample : MonoBehaviour { public delegate void OnButtonClickDelegate(string buttonName); public static OnButtonClickDelegate OnButtonClick; public void OnButtonClicked(string buttonName) { if (OnButtonClick != null) { OnButtonClick(buttonName); } } } ``` 在iOS中,需要使用UnityBinding文件中的方法来绑定委托。以下是一个示例代码,演示如何绑定上述的OnButtonClick委托: ``` #import "UnityInterface.h" #import "UnityAppController.h" @interface iOSUnityBridge : UnityAppController<UIApplicationDelegate> @end @implementation iOSUnityBridge - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { // Initialize Unity UnityInit([[[NSBundle mainBundle] bundlePath] UTF8String], "AppController", [self getUnityAppCommandLine]); // Add Unity view to the current view controller UIView* unityView = (__bridge UIView*)UnityGetGLView(); UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; [rootViewController.view addSubview:unityView]; // Bind delegate UnitySendMessage("DelegateExample", "set_OnButtonClickDelegate", "iOSUnityBridge", 0); return YES; } - (void)onButtonClick:(const char*)buttonName { NSString* name = [NSString stringWithUTF8String:buttonName]; NSLog(@"Button clicked: %@", name); // Call Unity delegate UnitySendMessage("DelegateExample", "OnButtonClick", buttonName); } @end ``` 在上述代码中,UnitySendMessage函数用于调用Unity中的set_OnButtonClickDelegate方法,将iOS中的iOSUnityBridge类绑定到Unity的OnButtonClick委托上。然后,在onButtonClick方法中,调用UnitySendMessage函数,触发Unity的OnButtonClick委托。 需要注意的是,以上代码只是一个简单的示例,实际实现中还需要考虑更多的细节和异常情况。同时,UnityBinding文件中的方法和接口可能会随着Unity版本的不同而有所变化,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值