委托代理的使用
写协议
使用协议
委托实现
1.写协议
@protocol PersonalDelegate <NSObject>
@required
-(void)PersonalEat;
@optional
-(void)personalWalk;
-(void)personalTalk;
-(void)personalWork;
@end
2.使用协议@protocol PersonalDelegate;
@interface Example : NSObject
@property (nonatomic, weak) id<PersonalDelegate> delegate;
@end
3.设置委托跟实现#import "ViewController.h"
#import "Example.h"
@interface ViewController ()<PersonalDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Example *object=[[Example alloc]init];
object.delegate=self;
// Do any additional setup after loading the view, typically from a nib.
}
#pragma - mark - PersonalDelegate
-(void)PersonalEat{
NSLog(@"i can eat");
}
-(void)personalWalk{
NSLog(@"i can walk");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end