iOS 多界面傳值之--Block(代碼塊)傳值

Block傳值

一般應用於逆向傳值,即第二界面向第一界面傳值,我們需要記住的兩點:
1.要在第二個界面(SecondViewController.h)定義一個Block:
2.在第一個界面(ViewController.m)跳轉第二個界面的方法中我們爲block屬性賦值完成block傳值:

3.首先來到SecondViewController.h定義一個代碼塊

#import <UIKit/UIKit.h>

//定義Block
typedef void (^PushBlock)(NSString *);

//聲明Block屬性
@interface SecondViewController : UIViewController

@property (nonatomic,strong) PushBlock pushValueString;

@end

4.在SecondViewController,m裏面設置UITextField 和 UIbutton 成爲屬性,然後在viewDidLoad 方法裏面分別創建一個textField 和 一個button


#import "SecondViewController.h"

@interface SecondViewController ()


@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) NSString *textString;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
    _textF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textF];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
    btn.backgroundColor = [UIColor purpleColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
    btn.tintColor = [UIColor whiteColor];
    [btn setTitle:@"Back" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

5.實現button裏面的點擊事件的方法,並且在這方法裏面實現Block的核心代碼

-(void)backAction:(UIButton *)sender{
//    核心代碼
    _pushValueString (_textF.text);
    [self dismissViewControllerAnimated:YES completion:nil];


}

6.回到ViewController裏面,首先我們需要導入一下頭文件#import “SecondViewController.h”,並且創建一個UITextField 和 UIbutton

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()

@property (nonatomic,strong)UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    /**
     *  block傳值(場景)一般用於逆向傳值,即第二個界面傳值給第一個界面
        1.要在第二個界面(SecondViewController.h)定義一個Block:
        2.要在第一個界面(ViewController.m)跳轉第二個界面的方法中我們爲block屬性賦值完成block傳值:
     */


    self.view.backgroundColor = [UIColor greenColor];
    self.title = @"Block傳值";
     _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
    btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
    btn.backgroundColor = [UIColor blueColor];
    btn.tintColor = [UIColor whiteColor];
    [btn setTitle:@"Next" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];


}

7.實現一下button的點擊事件的方法,並在這個方法裏面實現跳轉界面和block的核心代碼屬性賦值

-(void)nextAction:(UIButton *)sender{

    SecondViewController *secondVC = [[SecondViewController alloc]init];
//    核心代碼爲block屬性賦值
    secondVC.pushValueString = ^(NSString *string){
        _textField.text = string;

    };
    [self presentViewController:secondVC animated:YES completion:nil];



}

8.由於我們用的是模態視圖進行頁面之間的跳轉,所以就不需要去創建UINavigationController了.模擬器運行效果如下
這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章