zoukankan      html  css  js  c++  java
  • IOS 代码块传值

    #import <UIKit/UIKit.h>
    typedef void (^MyBlock)(NSString*);
    @interface SecondViewController : UIViewController
    
    @property (retain,nonatomic)UITextField* myTextField;
    @property(copy,nonatomic)MyBlock block;
    -(SecondViewController*)initWithBlock:(MyBlock)block;
    
    
    
    @end
    
    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    -(SecondViewController *)initWithBlock:(MyBlock)block
    {
        if (self=[super init]) {
            self.block=block;
        }
        return self;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton* btnTest=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        btnTest.frame=CGRectMake(120, 130, 80, 30) ;
        [btnTest setTitle:@"Test" forState:UIControlStateNormal];
        
        [btnTest addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btnTest];
        
        UITextField* textField=[[UITextField alloc]initWithFrame:CGRectMake(120, 80, 80, 30)];
        textField.borderStyle=UITextBorderStyleRoundedRect;
        [self.view addSubview:textField];
        self.myTextField=textField;
    }
    -(void)back{
        if (self.block) {
            self.block(self.myTextField.text);
        }
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    
    
    
    #import "ViewController.h"
    #import "SecondViewController.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIBarButtonItem* btnName=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(next)];
        self.navigationItem.rightBarButtonItem=btnName;
        // Do any additional setup after loading the view, typically from a nib.
    }
    -(void)next{
        SecondViewController* secondVC=[[SecondViewController alloc]initWithBlock:^(NSString* str){
            NSLog(@"%@",str);
            self.title=str;
        }];
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    输入一个nxn矩阵各元素的值,球出两条对角线元素之和
    打印杨辉三角
    编写一个函数,实现两个字符串的连接功能
    字符串置换。将字符串s中的出现的字符s1用字符s2置换
    有一行文字,要求删去其中某个字符
    自定义函数delstr()的功能是删去字符串s1中所有的"*"
    用微软的kestrel在Linux上利用Apache架设Asp.Net Core环境
    2012年8月14日 星期二 equals()方法 (冲突备份)
    jquery 操作DOM 案例
    FileUpload 控件上传图片和文件
  • 原文地址:https://www.cnblogs.com/mojiewei/p/5050337.html
Copyright © 2011-2022 走看看