iOS - block、代理、通知

一、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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值