zoukankan      html  css  js  c++  java
  • iOS基础之界面通信

      在这里讲的界面通信有三种:属性传值、协议传值和block传值

      首先我们来讲属性传值,属性传值用于从第一个页面传值到第二个页面,以此类推。

      代码演示:

    //一.在第二个视图控制器里声明一个属性contends
    @interface SecondViewController:UIViewController
    @property(nonatomic,copy)NSString *contents;
    @end
    
    //二.在点击FirstViewController按钮的方法里给SecondViewControoler的contents属性赋值
    - (void)buttonAction:(UIButton *)button{
      SecondViewController *secondVC = [[SecondViewController alloc]init];
    secondVC.contents =se;f.label.text;
    [self.navigationController pushViewController:secondVC animated:YES];  
    }
    
    //三.在SecondViewController使用contents属性给textField赋值
    @implementation SecondViewController
    - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(85,200,200,40)];
    self.textField.placeholder=@"请输入内容";
    self.textField.text = self.contents;
    [self.view addSubview:self.textField];
    }

      接下来是协议传值:

      1.声明协议

      2.声明代理

      3.执行协议方法

      4.签订协议

      5.指定代理

      6.实现协议方法

      

    //  .h 内部的声明部分  (即:fourthController )
    
    //1 声明协议
    
    //UI中的协议名称为,当前类名 + Delegate
    
    @protocol FourthViewControllerDelegate <NSObject>
    
     //不加说明:默认是必须实现的方法
    
    - (void)pushValue:(NSString *)text uicolor:(UIColor *)color;
    
    @end 
    
    @interface FourthViewController : UIViewController 
    
    //2 声明协议的代理对象
    
    @property (nonatomic, assign)   id <FourthViewControllerDelegate>  delegate;//代理对象
    
    @end
    
     
    // .m 内部的实现
    
    // 3, 执行协议的方法   (写在具体需要传值的方法内部)
    
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pushValue: uicolor:)] ) {  //代理不为空  代理内容接受
            [self.delegate pushValue:self.field.text uicolor:self.view.backgroundColor];//代理执行此方法,并且此时实现需要传的值
        }
    // 接收传值的内部 的   实现部分
    // 4 接受代理
    
    @interface ThirdViewController : UIViewController<FourthViewControllerDelegate>
    
    // 5 指定代理对象为当前的视图控制器
    
    fourthController.delegate = self;
    
    // 6 实现声明的协议方法
    
    //6 实现协议方法(写在当前视图控制器的  .m  内部)
    
     
    
    - (void)pushValue:(NSString *)text uicolor:(UIColor *)color{    
    
        self.label.text = text;
    
        self.view.backgroundColor = color;
    
    }

      

      最后是Block传值:

      block是匿名函数,能够实现函数回调功能,用于页面之间通信,同时可以进行传值。

      演示代码:

      

    #import <Foundation/Foundation.h>
    typedef void (^String)(NSString *); 
    @interface AppTool : NSObject 
    - (void)sendNumber:(NSInteger)number andBlock:(String)block;//Block类型作为参数使用 
    
    @end
    
    
    // .m 实现内容
    
    #import "AppTool.h" 
    
    @implementation AppTool 
    
    - (void)sendNumber:(NSInteger)number andBlock:(String)block{    
    
        NSString *string = [NSString stringWithFormat:@"%ld",number];//把数值转化为字符串
    
        block(string);//把字符作为Block的参数
    
    }
    
    @end
    
    
       //首先实例化一个对象
    
        AppTool *tool = [[AppTool alloc] init];
    
        //使用声明好的方法,实现赋值
    
        [tool sendNumber:10086 andBlock:^(NSString *str) {
            self.label.text = str;
        }];
    
     
    
    //SecondController的内部的写法
    
    //   .h  内部的声明
    
    typedef void (^StringC)(NSString *,UIColor *);//冲定义
    
    @interface SecondViewController : UIViewController
    
    @property (nonatomic, copy) StringC stringC;//一定要使用 copy
    
    @end
    
    //   .m  内部
    
     //和协议的使用相似
    
        if(self.stringC != nil){
    
            self.stringC(self.field.text,self.view.backgroundColor);//传值是 field的内容和背景颜色。。和函数的使用相似
    
        }
    
    2.2  具体的实现赋值(在接收数值的界面 实现Block)
    
    secondController.stringC = ^(NSString *str, UIColor *color){  
    
            self.label.text = str;
    
            self.view.backgroundColor = color;
    
        }; //实现了传值给 self 的内部控件 
    
     

      对比协议传值和Block传值,两者也有很多相似之处。

  • 相关阅读:
    Codeforces Round #547 F1&F2. Same Sum Blocks(贪心)
    Codeforces Round #547 D. Colored Boots(贪心)
    Codeforces Round #547 C. Polycarp Restores Permutation(二分枚举/数学+模拟)
    CCF 201812-4 数据中心(最小生成树)
    CCF【小明放学&小明上学】
    TIME_WAIT状态
    ping的详细过程
    两段不相邻子段和之和最大
    神水一题之“Who's in the Middle”
    日进一步之“A Knight's Journey”
  • 原文地址:https://www.cnblogs.com/16-jkd/p/5205353.html
Copyright © 2011-2022 走看看