• dismissViewControllerAnimated后 通过completion传值给上一个父视图方法


    视 图firstView和secendView,点击firstView上面的按钮presentviewcontroller出 secendView;secendView上有个按钮,点击按钮dismissViewControllerAnimated,并将某个值传给 firstView,或不直接在firstView里面的viewWillAppear里面调用方法,而是直接通过在 dismissViewControllerAnimated completion里面编辑代码块调用firstView里面的任何方法,该怎么做?

    这个问题其实并不复杂,如果你知道如何使用NSNotificationCenter实现起来还是非常简单的。

    先说一下,secendView在dismissViewControllerAnimated后,如何在进入firstView后,自动调用firstView里面的任何方法
    第一步:在secendView里面,点击按钮时调用一个方法,该方法为:
    -(void)secendAction{
    [self dismissViewControllerAnimated:YES completion:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self];
        }];

    }
    上面代码是将secendView dismissViewControllerAnimated掉,然后自动注册一个名为do的通知
    注册了这个名为的通知,你就可以在任何.m文件里面通过以下代码调用到了:
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
               selector:@selector(handleColorChange:)
                   name:@"do"
                 object:nil];
    上面的代码的意思就是,先找到已经注册过的名为do的通知,然后再自动调用handleColorChange去处理,
    所以:
    第二步:在firstView里面的viewWillAppear方法里面写入以下代码:
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
               selector:@selector(handleColorChange:)
                   name:@"do"
                 object:nil];
    handleColorChange方法为:
    -(void)handleColorChange:(id)sender{
        [self firstView里面方法]

    看明白了吧?在secendView里面我们不直接调用firstView里面的方法,而是通过通知来让firstView自动调用自己里面的某个方法。
    通过通知可以让不同.m文件之间进行方法和参数的传递

    ok就下来说一下如何在dismissViewControllerAnimated后将secendView里面的值传递给firstView
    第一步:在secendView里面,点击按钮时调用一个方法,该方法为:
    -(void)secendAction{
    [self dismissViewControllerAnimated:YES completion:^{
            [tools showToast:@"图片信息提交成功" withTime:1500 withPosition:iToastGravityCenter];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self userInfo:dictionary];
        }]; 

    }
    userInfo:dictionary里面的dictionary就是你要传递的字典对象的值
    第二步:在firstView里面的viewWillAppear方法里面写入以下代码:
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
               selector:@selector(handleColorChange:)
                   name:@"do"
                 object:nil];
    handleColorChange方法为:
    -(void)handleColorChange:(NSNotification*)sender{
    NSLog(@"%@",sender);
        [self firstView里面方法]

    -(void)handleColorChange:(NSNotification*)sender里面的sender就是你在secendView里面所传递的字典对象的值,简单吧?!

  • 相关阅读:
    【xsy2506】 bipartite 并查集+线段树
    Linux K8s容器集群技术
    Linux 运维工作中的经典应用ansible(批量管理)Docker容器技术(环境的快速搭建)
    Linux Django项目部署
    Linux Django项目测试
    Linux 首先基本包安装(vim啊什么的),源,源优化,项目架构介绍, (LNMuWsgi)Django项目相关软件mysql,redies,python(相关模块)安装配置测试
    Linux centos系统安装后的基本配置,Linux命令
    Linux 虚拟机上安装linux系统 (ip:子网掩码,网关,dns,交换机,路由知识回顾)
    $ Django 调API的几种方式,django自定义错误响应
    $Django 路飞之显示视频,Redis存购物车数据,优惠卷生成表,优惠卷的一个领取表。(知识小回顾)
  • 原文地址:https://www.cnblogs.com/allanliu/p/4778361.html
Copyright © 2020-2023  润新知