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;

    
    

    }

     
  • 相关阅读:
    redis事务
    redis杂项
    redis的发布订阅
    redis持久化
    redis常用配置参考.txt
    python语法流程控制
    用户交互与数据类型
    编程语言与Python介绍
    计算机的五大组成部分
    jieba库的基本介绍及爬虫基本操作
  • 原文地址:https://www.cnblogs.com/dabaomo/p/5207277.html
Copyright © 2011-2022 走看看