zoukankan      html  css  js  c++  java
  • iOS block传值和属性传值

    第一个控制器:

    -(void)barAction:(UIBarButtonItem*)sender
    {

        NextViewController *next=[[NextViewController alloc]init];
        //拿当前页面的值传到后一个页面
        next.stringValue=self.rv.textField.text;//属性传值

        
        //block传值
        __weak RootViewController *weakSelf=self;//weakSelf可以在block中修改,__week改变相互持有的状态,避免释放的时候无法释放
        
        
        //block前面传后面
        //next.pv=^{
        //    return weakSelf.rv.textField.text;
        //};
        
        
        
        //block传值
        next.mb=^(NSString *str){
            weakSelf.rv.textField.text=str;
        };
        
        
        [self.navigationController pushViewController:next animated:YES];

    }

    第二个控制器:

    .h文件


    typedef void(^MyBlock)(NSString *str);//block传值,定义一个block块

    //typedef NSString* (^PassValue)();//block前面传后面

    @interface NextViewController : UIViewController

    //接受前一个页面传过来的值
    @property(nonatomic,strong)NSString *stringValue;//属性传值

    //block传值
    @property(nonatomic,copy)MyBlock mb;//block传值


    //@property(nonatomic,copy)PassValue pv;//block前面传后面

    .m文件

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UIBarButtonItem *bar=[[UIBarButtonItem alloc]initWithTitle:@"退回" style: UIBarButtonItemStyleDone target:self action:@selector(barAction:)];
        
        self.navigationItem.leftBarButtonItem=bar;
        
        //用前一个页面传过来的值赋给当前页面
        self.nv.textField.text=self.stringValue;//属性传值
        //self.nv.textField.text=self.pv;//block前面传后面
        
    }

    -(void)barAction:(UIBarButtonItem*)sender
    {
        self.mb(self.nv.textField.text);//block传值
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }

  • 相关阅读:
    Linux学习笔记总结--CentOS 设置静态IP
    LAMP环境部署总结
    expect批量分发公钥
    CentOS6.5一键安装MySQL5.5.32(源码编译)
    CentOS6.5 一键部署运行环境shell脚本
    CentOS 更新yum源
    centos 6.6编译安装nginx
    安装Oracle数据库和PLSQL连接数据库
    ABAP 取字段的简短描述
    ABAP OLE常用方法和属性
  • 原文地址:https://www.cnblogs.com/-ios/p/4672870.html
Copyright © 2011-2022 走看看