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