@protocol Buttonelegate;
2、定义协议变量和协议方法
@property(nonatomic,assign) id<</span>Buttonelegate>delegate;
@end
@protocol Buttonelegate <<span style="color: #703daa">NSObject>
-(void)buttonsave:(NSIndexPath *)IndexPath;
@end
3、在委托类中添加协议,设置协议,实现协议方法
@interface firstview : UITableViewController<</span>Buttonelegate>
UITableViewCell *cell=sender;
secondview *view=segue.destinationViewController;
view.delegate=self;
-(void)buttonsave:(NSIndexPath *)IndexPath
{
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:IndexPath];
cell.detailTextLabel.text=@"已评估";
[self.navigationController popViewControllerAnimated:YES];
}
4、调用协议方法
if ([self.delegate respondsToSelector:@selector(buttonsave:)])
{
[self.delegate buttonsave:path];
}
代理是一对一的关系,而通知则是一对多的关系。
代理的实现代码比较多,简单得传值可以用block代替
//定义声明block 相当于delegate的声明
@property (nonatomic, copy) void(^doTransferMsg)(NSString *_msg);
//发送消息
if(self.doTransferMsg)
{
self.doTransferMsg(oneModel.chinese);
self.doTransferMsg=nil;
}
相当于
if ([self.delegate respondsToSelector:@selector(buttonsave:)])
{
[self.delegate buttonsave:path];
}
//接受消息,预设
__weak CityViewController *cityVC=[HUDTooles getinstantiateViewControllerWithIdentifier:@"CityView" andstoryboardName:@"Main"];
cityVC.list=citys;
cityVC.tablelist=dictionary;
[cityVC setDoTransferMsg:^(NSString * string) {
[sender setTitle:string forState:UIControlStateNormal];
[cityVC dismissViewControllerAnimated:YES completion:^{
//
}];
}];
[self presentViewController:cityVC animated:YES completion:^{
//
}];
相当于
UITableViewCell *cell=sender;
secondview *view=segue.destinationViewController;
view.delegate=self;