美文网首页
委托范例

委托范例

作者: zhujames | 来源:发表于2018-08-25 18:07 被阅读0次

    Class.h

    #import <Foundation/Foundation.h>
    //委托的协议定义
    @protocol doActionDelegate <NSObject>
    - (void)doAction;
    @end
    
    @interface Class : NSObject
    //委托变量定义
    @property (nonatomic, weak) id<doActionDelegate> delegate;
    
    - (void) startAction;
       
    @end
    

    Class.m

    #import "Class.h"
    
    @implementation Class
    - (void) startAction
    {
        [self.delegate doAction];
    }
    @end
    

    ViewController.h

    #import "Class.h"
    
    @interface ViewController : UIViewController<doActionDelegate>
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        Class *obj = [[Class alloc] init];
        obj.delegate = self; //设置委托实例
        [obj startAction];
    }
    
    - (void)doAction
    {
        //处理要做的事
        NSLog(@"代理协议实现");
    }
    

    相关文章

      网友评论

          本文标题:委托范例

          本文链接:https://www.haomeiwen.com/subject/oppgiftx.html