1.委托 的作用 :回传值(回调);
2.当我们声明遵循了协议的属性时,属性的关键字weak或assign weak,assign 的作用是 避免循环引用
3.委托的特点:一对一
4.用途是用在有上下级关系的两个view(类),不能跨级调用。(即a-b时需要在b中添加一个协议 在a中初始化b,用self,
创建:
1.//在被委托者的声明文件(.h)中进行协议的声明,然后创建遵循协议的属性,如上2所示
#import <Foundation/Foundation.h>
@protocol PaymentDelegate <NSObject>
-(void)payment:(float)money;
@protocol PaymentDelegate <NSObject>
-(void)payment:(float)money;
@end
@interface Mediation : NSObject
@property (nonatomic, weak) id <PaymentDelegate> delegate;
-(void)handle;
@property (nonatomic, weak) id <PaymentDelegate> delegate;
-(void)handle;
@end
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
#import "Mediation.h"
@implementation Mediation
-(void)handle{
NSLog(@"找到房源,请给钱");
[_delegate payment:10.0];
}
@implementation Mediation
-(void)handle{
NSLog(@"找到房源,请给钱");
[_delegate payment:10.0];
}
@end
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
//发布委托 作用:委托谁帮我做事(调用协议方法)
#import "Buyer.h"
@implementation Buyer
-(void)findMediation{
NSLog(@"我需要房源");
Mediation *m = [[Mediation alloc] init];
m.delegate = self;
[m handle];
NSLog(@"我需要房源");
Mediation *m = [[Mediation alloc] init];
m.delegate = self;
[m handle];
}
-(void)payment:(float)money{
NSLog(@"付钱:%.2f",money);
}
@end
NSLog(@"付钱:%.2f",money);
}
@end
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
#import <Foundation/Foundation.h>
#import "Mediation.h"
@interface Buyer : NSObject<PaymentDelegate>
-(void)findMediation;
#import "Mediation.h"
@interface Buyer : NSObject<PaymentDelegate>
-(void)findMediation;
@end
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
1。回传值 的调用
传什么内容就要用什么类型的值接收例
//传值:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
detailViewController *detaqil =[[detailViewController alloc]init];
detaqil.delegate = self;
detaqil.perrr =arr[indexPath.row];//关键的地方传值的类型 此时的perrr相当于数组的一个对象,取值的话用perrr.数组的内容
sedter=indexPath.row;
[self.navigationController pushViewController:detaqil animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.navigationController pushViewController:detaqil animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
self.view.backgroundColor = [UIColor whiteColor];
nametext = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
nametext.borderStyle =UITextBorderStyleRoundedRect;
nametext = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
nametext.borderStyle =UITextBorderStyleRoundedRect;
nametext.backgroundColor = [UIColor whiteColor];
nametext.text =_perrr.name;//(perrr.name 相当于单一数组中的元素之一
nametext.userInteractionEnabled =NO;
[self.view addSubview:nametext];
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//删行
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[arr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
}
-(void)ediddd:(NSString *)name andimage:(NSString *)phone{
Person *prr =[[Person alloc]init];
prr.name =name;
prr.phone =phone;
[arr replaceObjectAtIndex:sedter withObject:prr];
[table reloadData];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//删行
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[arr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
}
-(void)ediddd:(NSString *)name andimage:(NSString *)phone{
Person *prr =[[Person alloc]init];
prr.name =name;
prr.phone =phone;
[arr replaceObjectAtIndex:sedter withObject:prr];
[table reloadData];
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
//增加值
-(void)addindex{
AddViewController *addview =[[AddViewController alloc]init];
addview.delegate =self;
[self.navigationController pushViewController:addview animated:YES];
}
-(void)addcontrioldelegate:(NSString *)name andphone:(NSString *)phone{
Person *peerr = [[Person alloc]init];
peerr.name =name;
peerr.phone =phone;
[arr addObject:peerr];
[table reloadData];
-(void)addindex{
AddViewController *addview =[[AddViewController alloc]init];
addview.delegate =self;
[self.navigationController pushViewController:addview animated:YES];
}
-(void)addcontrioldelegate:(NSString *)name andphone:(NSString *)phone{
Person *peerr = [[Person alloc]init];
peerr.name =name;
peerr.phone =phone;
[arr addObject:peerr];
[table reloadData];
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
9.委托用于自定义控件
//委托用于自定义控件
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()<BtnPressedDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(40, 80, 200, 200)];
myView.delegate = self;
[self.view addSubview:myView];
}
-(void)btnPressed{
self.view.backgroundColor = [UIColor grayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()<BtnPressedDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(40, 80, 200, 200)];
myView.delegate = self;
[self.view addSubview:myView];
}
-(void)btnPressed{
self.view.backgroundColor = [UIColor grayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #import <UIKit/UIKit.h>
@protocol BtnPressedDelegate <NSObject>
-(void)btnPressed;
@end
@interface MyView : UIView
@property (nonatomic, weak) id<BtnPressedDelegate> delegate;
@protocol BtnPressedDelegate <NSObject>
-(void)btnPressed;
@end
@interface MyView : UIView
@property (nonatomic, weak) id<BtnPressedDelegate> delegate;
@end>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #import "MyView.h"
@implementation MyView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor brownColor];
self.layer.cornerRadius = 5;
self.layer.masksToBounds = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 40, 80, 80);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
-(void)btnPressed{
NSLog(@"按钮被点击");
[_delegate btnPressed];
}
@implementation MyView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor brownColor];
self.layer.cornerRadius = 5;
self.layer.masksToBounds = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 40, 80, 80);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
-(void)btnPressed{
NSLog(@"按钮被点击");
[_delegate btnPressed];
}
@end