代理设计模式简单格式(备忘)
A控制器充当B控制器的代理:(B页面的数据反传给A页面进行数据的更新,一般A跳转到B的这种情况下需要使用代理方法)
B控制器.h文件中的代码如下:
#import <UIKit/UIKit.h>
@protocol SearchDestinationDelegate <NSObject>
- (void)searchDestinationWithLocation:(CLLocationCoordinate2D)secrchLocationCoordinate;
@end
@interface SearchDestinationVC : UIViewController
@property (nonatomic, weak) id<SearchDestinationDelegate> searchLocationDelegate;
@end
B控制器.m文件中的代码如下:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.searchLocationDelegate respondsToSelector:@selector(searchDestinationWithLocation:)]) {
[self.searchLocationDelegate searchDestinationWithLocation:self.endUserLocation];
}
[self.navigationController popViewControllerAnimated:YES];
}
A页面需要实现相应的代理,和代理方法,.m文件中的代码如下
//跳转到B页面的时候,让A页面充当B页面的代理<SearchDestinationDelegate>
SearchDestinationVC *searchVC = [[SearchDestinationVC alloc]init];
searchVC.searchLocationDelegate = self;
[self.navigationController pushViewController:searchVC animated:NO];
//实现代理中的方法
-(void)searchDestinationWithLocation:(CLLocationCoordinate2D)secrchLocationCoordinate
{
//拿到B页面穿过来的数据,在A页面进行需要的设置
}