iOS block 回調

來源:互聯網
上載者:User

標籤:

說明:本文是在瞭解block基礎知識基礎上的應用,假定閱讀者已經具備block基礎知識。

目的:通過block回調方式將SecondViewController中的值傳入到ViewController中,在某些時候,通過block回調,可以避免delegate的繁瑣

1,建立Single View Application工程,建立SecondViewController

2,在ViewController中添加UILabel用於顯示SecondViewController傳過來的值,把label設定為成員變數,添加一個UIButton,用於點擊跳轉到SecondViewController

@interface ViewController (){    UILabel *_label;}@end- (void)viewDidLoad {    [super viewDidLoad];    //添加UIButton,用於跳轉到SecondViewController    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 44)];    [btn setTitle:@"ToSecondViewController" forState:UIControlStateNormal];    btn.backgroundColor = [UIColor grayColor];    [btn addTarget:self action:@selector(toSecondViewController) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];        //添加UILabel,用於顯示SecondViewController傳過來的值    _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 44)];    _label.backgroundColor = [UIColor yellowColor];    [self.view addSubview:_label];}

 

3,在SecondViewController.m中添加一個UITextField,設定為成員變數,用於輸入值,添加一個UIButton,用於點擊跳回ViewController

@interface SecondViewController (){    UITextField *_textField;}@end- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    //添加一個按鈕用於返回ViewController    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 44)];    [btn setTitle:@"BackToViewController" forState:UIControlStateNormal];    btn.backgroundColor = [UIColor blueColor];    [btn addTarget:self action:@selector(backToViewController) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];        //添加一個UITextField    _textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 300, 44)];    _textField.backgroundColor = [UIColor greenColor];    [self.view addSubview:_textField];}

4,在SecondViewController.h中,利用typedef定義一個block,並設定成property,同時聲明一個回調方法

#import <UIKit/UIKit.h>//定義一個blocktypedef void (^ReturnValueBlock)(NSString *value);@interface SecondViewController : UIViewController@property (nonatomic,copy)ReturnValueBlock returnValueBlock;//回調方法- (void)returnValue:(ReturnValueBlock)block;@end

5,實現回調方法,並且在點擊返回ViewController按鈕時調用

- (void)backToViewController {    //當點擊返回ViewController時回調    if (self.returnValueBlock != nil) {        self.returnValueBlock(_textField.text);    }        [self dismissViewControllerAnimated:YES completion:nil];}- (void)returnValue:(ReturnValueBlock)block {    self.returnValueBlock = block;}

6,在ViewController.m中使用block方法為label賦值

- (void)toSecondViewController {    SecondViewController *second = [[SecondViewController alloc] init];    [second returnValue:^(NSString *value) {        _label.text = value;    }];    [self presentViewController:second animated:YES completion:nil];}

源碼地址:https://github.com/rokistar/PassValueUsingBlock

iOS block 回調

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.