一、block
LXFColorViewController.h
typedef void(^SelectColor)(UIColor*color);
/** 选择颜色的回调block */
@property(nonatomic, copy) SelectColor selectColor;
LXFColorViewController.m
在适当的位置传入对应的值
if (self.selectColor) {
self.selectColor(selectColor);
}
ViewController.m
该.m文件中创建了相应的控制器colorVc,并使用传递来过的值
LXFColorViewController *colorVc = [[LXFColorViewControlleralloc] init];
colorVc.selectColor =^(UIColor *color) {
self.view.backgroundColor = color;
[colorPover dismissPopoverAnimated:YES];
};
如果实在想不起来block怎么写,可以直接打inlineBlock
有一点需要注意:block定义为属性/变量与定义为方法的参数的写法是不一样的
属性/变量:
@property(nonatomic, copy) void (^CompletionBlock)();
方法参数:
- (void)cancelWithCompletionBlock:(void (^)())CompletionBlock
二、代理
1、成为代理
2、遵守协议
3、实现代理方法
三、通知
接收通知并执行操作的控制器
- (void)viewDidLoad{
[superviewDidLoad];
// 监听通知
[selfsetupNotifications];
}
// 监听通知
-(void)setupNotifications
{
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(categoryDidChange:) name:LXFCategoryDidChangeNotificationobject:nil];
}
-(void)dealloc
{
//移除通知
[[NSNotificationCenterdefaultCenter] removeObserver:self];
}
#pragma mark - 监听到通知之后实现的方法
-(void)categoryDidChange:(NSNotification *)note
{
// 1.获取信息
LXFCategory *category = note.userInfo[LXFCategoryDidChangeNotificationCategoryKey];
NSString *subCategory = note.userInfo[LXFCategoryDidChangeNotificationSubcategoryKey];
// 2.取出对应的顶部的View
LXFTopItemView *categoryItemView = (LXFTopItemView *)self.categoryItem.customView;
if (!subCategory) {
[categoryItemView setTitle:@"美团"];
[categoryItemView setSubtitle:category.name];
} else {
[categoryItemView setTitle:category.name];
[categoryItemView setSubtitle:subCategory];
}
[categoryItemView setIconButtonWithImage:category.iconhighImage:category.highlighted_icon];
// 3.退出控制器
[self.categoryVcdismissViewControllerAnimated:YEScompletion:nil];
}
发送通知的控制器
// 1.通知携带的信息
NSDictionary*userInfo = @{
LXFCategoryDidChangeNotificationCategoryKey : category,
LXFCategoryDidChangeNotificationSubcategoryKey : subCategory
};
// 2.发出通知
[[NSNotificationCenterdefaultCenter] postNotificationName:LXFCategoryDidChangeNotificationobject:niluserInfo:userInfo];