界面传值

作者: Tanyfi | 来源:发表于2016-08-16 10:53 被阅读19次

Block传值

  • 后->前
    例如有2个界面:FirstViewController 和 SecondViewController
    界面元素:
    FirstViewController: 一个Label 和 Button
    SecondViewController: 一个TextField 和 Button

  • 在SecondViewController.h中

  • 重定义block

typedef void (^SecondBlock) (NSString *);
  • 声明一个block 属性值
@property (nonatomic , copy) SecondBlock tzf;
  • 在SecondViewController.m中
  • 点击按钮实现的方法:
    - (void)back {
          if (self.tf != nil) {
          self.tzf(self.tf.text);
          }
          [self.navigationController popViewControllerAnimated:YES];
    }
  • FirstViewController.m 按钮方法中
 - (void)next {
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.tzf = ^(NSString *str){
        self.label.text = str;
    };
    [self.navigationController pushViewController:secondVC animated:YES];
}

代理传值

后- > 前

例如有2个界面:ThirdViewController 和 FourthViewController
界面元素:
ThirdViewController: 一个Label 和 Button
FourthViewController: 一个TextField 和 Button

步骤:<前3步 都是在后面的控制器中, 后面3步在第一个控制器>
  • 声明协议 <协议方法等>
  • 声明�代理属性
  • 代理对象执行协议方法
  • 指定代理对象
  • 接收协议
  • 实现协议方法
在第二个控制器FourthViewController.h中
  • 声明协议 <协议方法等>
  • 声明�代理属性
  //1、 声明协议 <协议方法等>
  @protocol FourthViewControllerDelegate <NSObject>
     - (void)sendString:(NSString *)string;
  @end
  @interface FourthViewController : UIViewController
  //2、声明代理属性
     @property (nonatomic, assign) id<FourthViewControllerDelegate>    delegate;

  @end
在第二个控制器FourthViewController.m 返回按钮的方法中
- (void)back {
    //3、代理对象执行协议方法
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(sendString:)]) {
        [self.delegate sendString:self.textField.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
在第 一个控制器ThirdViewController.m 按钮的方法中
- (void)push {
    FourthViewController *fourthVC = [[FourthViewController alloc] init];
    //4、指定代理对象
    fourthVC.delegate = self;
    [self.navigationController pushViewController:fourthVC animated:YES];
}
//5、接收协议
@interface ThirdViewController ()<FourthViewControllerDelegate>

@property (nonatomic, retain) UILabel *label;

@end
//6、实现协议方法
//为标签赋值文本的方法
- (void)sendString:(NSString *)string {
    self.label.text = string;
}


相关文章

  • iOS中界面传值的几种方式

    1.属性传值 属性传值适用于顺序传值,从前面的界面传值给后面的界面。 2.代理传值 用代理的方式实现界面间传值稍微...

  • IOS开发 多界面传值

    本节学习内容: 1.多界面传值的基本概念 2.多界面传值的方法 3.多界面传值的应用 【多界面传值 属性】 cha...

  • ios界面传值2016.5

    五种方法 1.属性传值,适合界面A到界面B的传值2.单例, 多个界面传值3.通知 , 界面A跳...

  • iOS开发-属性、block、代理、通知传值

    传值在开发中我们会经常用到,传值又分为正向传值和反向传值。从界面一跳转到界面二且将值从界面一传递给界面二使用,称之...

  • iOS 属性、代理、通知、Block传值

    实际开发中,几乎到处都会有用到传值,而传值分为正向传值以及逆(反)向传值,比如从界面一调到界面二,并将值从界面一传...

  • swift中的协议代理传值

    协议代理传值一般使用在下级界面往上级界面传值的情况,这里将上级界面设定为A界面,下级界面设定为B界面。传值的具体操...

  • UI总结-界面传值

    UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值) 在编程过程中,界面传值是很重要的一部分,常...

  • iOS的5种传值

    (-)属性传值 属性传值(场景)一般用于正向传值,即第一个界面传值给第二个界面 属性传值是这几大传值中最简单的传值...

  • iOS页面间传值详解(一)

    一、A页面传值给B界面: 采用的传值方式:属性传值,在A页面跳转B界面的地方直接给B界面的属性赋值即可。 二、A页...

  • 微信小程序:界面传值、取值

    小程序界面传值 父级界面:A界面子级界面:B界面 一、url传值 详细的配置参数可以查看组件导航:navigato...

网友评论

  • lizhi_boy:简单粗暴,可以的
    Tanyfi: @荔枝_boy 哈哈!谢谢!站在大众的角度!写的比较通俗!
  • Booooooooom:懵逼之后的总结:smile:
    Tanyfi:@Booooooooom 哈哈!是啊!总结很重重要

本文标题:界面传值

本文链接:https://www.haomeiwen.com/subject/tjlpsttx.html