[iOS总结]delegate的学习

delegate:

平时我们一般都是用系统的控制或者模块的代理.比如table.现在我们要做一个自己的代理

先定义一个代理类.

//
//  HTMyclass2.h
//  testDelegate
//
//  Created by biostime on 14-6-10.
//  Copyright (c) 2014年 holdtime. All rights reserved.
//

#import <Foundation/Foundation.h>
@protocol MyDelegate <NSObject>

- (void)didJobs;

@end

@interface HTMyclass2 : NSObject

@property(nonatomic, weak) id<MyDelegate> delegate;

@end

//
//  HTMyclass2.m
//  testDelegate
//
//  Created by biostime on 14-6-10.
//  Copyright (c) 2014年 holdtime. All rights reserved.
//

#import "HTMyclass2.h"

@implementation HTMyclass2

@synthesize delegate;

//满足某个条件就让调用代理的方法
-(id)init{
    [delegate didJobs];//调用代理的方法
    
    return self;
}
@end



再写一个实现代理的类.

//
//  HTMyclass.h
//  testDelegate
//
//  Created by biostime on 14-6-10.
//  Copyright (c) 2014年 holdtime. All rights reserved.
//

#import <Foundation/Foundation.h>
#include "HTMyclass2.h"

@interface HTMyclass : NSObject<MyDelegate>{
    
}

@end


//
//  HTMyclass.m
//  testDelegate
//
//  Created by biostime on 14-6-10.
//  Copyright (c) 2014年 holdtime. All rights reserved.
//

#import "HTMyclass.h"
#import "HTMyclass2.h"

@implementation HTMyclass

- (void)initView{
    HTMyclass2 *class2 = [[HTMyclass2 alloc]init];
    class2.delegate = self;
}

- (void)didJobs{
    NSLog(@"hello");
}
@end



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值