iOS——通知传值

本文介绍了在iOS开发中如何利用通知中心(NSNotification)进行跨页面的数据传递,特别是在删除操作后更新前一个界面的数据。首先在第一个界面注册通知,然后在第二个界面触发删除操作并发送通知,携带更新后的数组。最后在接收到通知时更新第一个界面的数据显示。这种方法适用于不希望使用代理或者Block的情况,提供了一种灵活的数据同步方式。
摘要由CSDN通过智能技术生成

通知传值

通知传值也是逆向传值的一种,通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面。

基本步骤

这里的使用背景:在第一个界面有三个数组分别存储姓名班级和成绩,显示在视图上,在第二个界面通过搜索学生姓名删除信息,将新的数组传给第一个界面,重新显示在视图上。

注册通知

在第一个界面跳转到第二个界面时,注册通知中心。

- (void) pressDelChange {
    DeleteViewController* deleteViewController = [[DeleteViewController alloc] init];
    deleteViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    deleteViewController.homeNameArray = _homeNameArray;
    deleteViewController.homeMarkArray = _homeMarkArray;
    deleteViewController.homeClassArray = _homeClassArray;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeArray:) name:@"ChangeArray" object:nil];
    [self presentViewController:deleteViewController animated:YES completion:nil];
}

这里要注意消息中心的姓名要和发送消息时的一直,不然会出现无法接受消息的问题,导致数组为空。

在第二个界面发送消息通知

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否删除该学生信息" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认"  style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.homeNameArray removeObjectAtIndex:i];
                [self.homeClassArray removeObjectAtIndex:i];
                [self.homeMarkArray removeObjectAtIndex:i];
                NSDictionary *diction = [[NSDictionary alloc]initWithObjectsAndKeys:self.homeNameArray,@"Array",self.homeClassArray,@"classArray",self.homeMarkArray, @"markArray", nil];
                NSLog(@"%@", diction[@"markArray"]);
                [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeArray" object:nil userInfo:diction];
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"  style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:sureAction];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
            return;

这里代码中使用了一个字典存储将要传递的数组,通过键来识别是哪一个。

实现通知中心内部的方法

所谓通知中心的方法,就是注册通知时的第二个参数。在这个方法里实现传值,并充值tableView

- (void) ChangeArray:(NSNotification *)notification {
    NSDictionary *dict = notification.userInfo;
    self.homeNameArray = dict[@"Array"];
    self.homeClassArray = dict[@"classArray"];
    self.homeMarkArray = dict[@"markArray"];
    NSLog(@"%ld", self.homeMarkArray.count);
    [self.tableView reloadData];
}

移除掉通知中心

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值