zoukankan      html  css  js  c++  java
  • 界面间传值--反向传值(代理,协议)及代码块回调

    1、

    (1) 第一步,制定协议

    //因为是ConfigViewController其他界面发送消息

    //  1. 告诉其他界面我要给你发送什么消息

    //  2. 确保其他实现了消息对应的方法

     

    // 声明协议和协议方法

    @protocol ConfigViewControllerDelegate <NSObject>

    // 最好以类型开头+方法

    // 遵守协议的其他类实现方法

    -(void)changeFontSize:(int)size;

    @end

    2、

    @interface ConfigViewController : UIViewController

    (2) 第二步,定义属性

    // 作用: 保存主界面的指针

    // 为什么保存, 最后给主界面发送消息修改字体

    // 细节1: weak 确保不会循环引用

    // 细节2: id 如果是id, 能传入任意界面了

    // 细节3: 遵守协议意味着可以任意对象

    @property (nonatomic,weak) id<ConfigViewControllerDelegate> delegate;

     

    @end

    // 通知代理

        [self.delegate changeFontSize:fontTextField.text.intValue]; 

    3、

    (3) 第三步,遵守协议 

    @interface MainViewController () <ConfigViewControllerDelegate>

    4

     (4) 第四步,设置成为代理

     ConfigViewController *cvc = [[ConfigViewController alloc] init];

        // 作用: 把当前界面的指针传到cvc

        // 为啥: 希望配置界面中字体改变通知主界面

        cvc.delegate = self;

    5、

    (5) 第五步,实现代理方法(具体方法的实现)

    //改变字体的方法

    -(void)changeFontSize:(int)size

    {

        novelLabel.font = [UIFont systemFontOfSize:size];

    }

     

    》在声明代理属性时

    // 不能使用strong,避免VC调用sVC,然后sVC再调用VC形成循环引用,内存无法释放

    @property (nonatomic,weak) id<SecondViewControllerDelegate> delegate;

     

    ====界面间的传值

      两个控制器间的传值、代码块回调

    Viewcontroller1 *vc1 = [[Viewcontroller1 alloc] init];

    [vc onSave:^{

                [self.navigationController popViewControllerAnimated:YES];     

            } error:^(NSString *error) {}];

     

    在另一个控制器中回调

    -(void)onSave:(void(^)())success error:(void(^)(NSString* error))error{

      success();

    }

     

     

    《《《《《《《《《《》》》》》》

    代码块回调

    [Tool2 getUrl:url params:param success:^(id resurt) {
            
            success(resurt);
            
            
        } failures:^(NSError *error) {
            failure(error);
        }];
    

    Tool2.h中添加代码

    + (void)getUrl:(NSString *)url params:(NSDictionary *)params success:(void (^)(id resurt))success failures:(void (^)(NSError *error))error;
    

    Tool2.m中添加方法的实现

    + (void)getUrl:(NSString *)url params:(NSDictionary *)params success:(void (^)(id resurt))success failures:(void (^)(NSError *error))error{
        
        success(@"要回调返回的内容");
    
    }
    

      

  • 相关阅读:
    好题Islands
    DB2的安装
    MariaDB存在的问题
    MariaDB 脚本
    SQL 执行顺序
    Maria数据库
    3 ignite windows 上安装
    Cassandra 学习七 cassandra研究
    Cassandra学习六 一些知识点
    Cassandra学习五 使用Key的正确姿势
  • 原文地址:https://www.cnblogs.com/h-tao/p/5151136.html
Copyright © 2011-2022 走看看