iOS NSNotificationCenter的简单使用

本文详细介绍了如何在iOS应用中使用通知中心实现不同组件间的通信。通过AppDelegate和ViewController注册监听特定的通知名,并在SecondViewController中触发这些通知。文章还展示了如何在界面上展示通知内容并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在工程的两个地方注册通知, 一个是AppDelegate, 模拟后台接受通知的处理, 一个是ViewController, 模拟UI接受通知的处理. 在另一个界面SecondViewController中设置一个按钮事件, 模拟通知的触发.


界面准备:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController中有一个button(进入下一页)和一个label(显示通知内容)

    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:@"进入下一页" forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    btn.frame = CGRectMake(100, 100, 200, 30);
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:btn];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 30)];
    label.tag = 9090;
    label.textColor = [UIColor blackColor];
    label.text = @"------";
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];

SecondViewController中有一个Button(按钮触发通知的发送)

    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:@"触发通知" forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    btn.frame = CGRectMake(100, 100, 150, 30);
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:btn];

// 用按钮事件模拟触发通知
- (void)btnAction:(UIButton *)btn {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_LABELTEXT" object:@"第二页的通知内容"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"APPDELEGATE_NOTIFICATION" object:@"通知Applegate执行事件"];
}

注册两个通知中心:

AppDelegate.m

//     注册通知 self接受名为@"APPDELEGATE_NOTIFICATION"的通知后,执行notification:方法
//     nil表示不限制通知的发送者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"APPDELEGATE_NOTIFICATION" object:nil];

// 接受对应通知之后执行的方法
- (void)notification:(NSNotification *)notification {
//    notification.name 通知的name
//    notification.object 通知的内容, 可自定义
    NSLog(@"%@", notification.object);
}


ViewController.m

// 注册通知 self接受名为@"CHANGE_LABELTEXT"的通知后,执行notification:方法
    // nil表示不限制通知的发送者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"CHANGE_LABELTEXT" object:nil];


// 接受对应通知之后执行的方法
- (void)notification:(NSNotification *)notification {
//    notification.name 通知的name
//    notification.object 通知的内容, 可自定义
    NSString *str = (NSString *)notification.object;
    ((UILabel *)[self.view viewWithTag:9090]).text = str;
}


注意:每注册一个通知, 就要在相应类的dealloc方法体类注销通知:

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


点击SecondViewController中的按钮时, 发送两个通知, 分别通知AppDelegate和ViewController进行打印和修改label的操作. 当然也可以一个通知多个地方监听, 执行不同操作.

源码下载

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值