iOSBlock回調(diào)

在第二個頁面的.h中

#import <UIKit/UIKit.h>
//塊語法回傳第一步,聲明
typedef void(^ShowTextBlock)(NSString *text);
@interface NextViewController : UIViewController
//快語法回傳第二步,聲明塊類型的對象
@property(nonatomic,copy) ShowTextBlock showTextBlock;
@end

在第二個頁面的.m中

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController
- (IBAction)clickReturn:(UITextField *)sender {
//塊語法回傳第三步,適當(dāng)?shù)奈恢谜{(diào)用
    _showTextBlock(sender.text);
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

在第一個頁面的.h中獲取:

#import "MyViewController.h"
#import "NextViewController.h"
@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation MyViewController
- (IBAction)click:(id)sender {
    NextViewController *nextVC = [NextViewController new];
//第四步:獲取到回調(diào)信息.
    nextVC.showTextBlock = ^(NSString *text){
        _label.text = text;
    };
    [self presentViewController:nextVC animated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容