zoukankan      html  css  js  c++  java
  • 界面通信之属性传值、代理传值

    界面通信有三种方法

    1、属性传值

    2、代理传值

    3、blcok传值

    先讲讲属性和传值和代理传值

    //属性传值
    - (void)sendValue { SecondViewController *secondVC = [[SecondViewController alloc] init]; secondVC.string = _textField.text;//这句是属性传值的核心,在需要接收值的界面声明一个字符串属性接受值 secondVC.color = self.view.backgroundColor; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release]; }

    属性传值虽然方便,但是属性传值只能从前往后传值,不能从后往前传值,那么就需要用代理传值和block传值方法了

    代理传值的步骤

    //1、声明协议,代理传值用于从后往前传值,所以后者应该是协议的拥有者,在后面的界面声明协议
    //UI中的协议名称为 当前类名 + Delegate
    @protocol FourthViewControllerDelegate <NSObject>
    
    - (void)pushValue:(NSString *)text color:(UIColor *)color;
    
    @end
    //2、声明代理
    @property (nonatomic, assign) id<FourthViewControllerDelegate> delegate;
    //3、执行协议方法
    - (void)back {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pushValue:color:)]) {
            [self.delegate pushValue:self.textField.text color:self.view.backgroundColor];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    //4、接收协议  接受协议的应该是需要接收值的界面,红色部分便是接收协议
    @interface ThirdViewController : UIViewController<FourthViewControllerDelegate>
    @end

    //5、设置代理

    - (void)next {

    
    

        FourthViewController *fourthVC = [[FourthViewController alloc] init];

    
    

        fourthVC.delegate = self;//指定第四个界面的代理对象为第三个视图控制器

    
    

        [self.navigationController pushViewController:fourthVC animated:YES];

    
    

        [fourthVC release];

    
    

    }

    //6、实现代理方法

    - (void)pushValue:(NSString *)text color:(UIColor *)color {

    
    

        self.label.text = text;

    
    

        self.view.backgroundColor = color;

    
    

    }

     
  • 相关阅读:
    docker 安装mysql5.7 加my.cnf
    docker安装redis 配置文件
    私库nexus 配置
    mysql 多个字段建立唯一索引
    scm 一些记录
    tomcat 线程数、NIO配置、内存配置
    为什么简单的一个select查询都要加上事务控制
    powerdeginer report layout
    powerdesigner-连接mysql
    [转载]如何让自己变得有趣
  • 原文地址:https://www.cnblogs.com/dabaomo/p/5207277.html
Copyright © 2011-2022 走看看