iOS笔记—Notification

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 01:32   21   0

Notification,一对多的情况下可以使用

//
//  ViewController.m
//   通知
//
//  Created by hhg on 15/9/28.
//  Copyright (c) 2015年 hhg. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor blueColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(centerFunc:) name:@"Notification" object:nil];

}
-(void)buttonClick:(UIButton *)button {
    [center postNotificationName:@"Notification" object:nil];
}

-(void)centerFunc:(NSNotificationCenter *)centerPart {

    if ([[centerPart valueForKey:@"name"] isEqualToString:@"Notification"]) {
        NSLog(@"Notification is action");
    } else {
        NSLog(@"不匹配");
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP