block 代碼塊 反向傳值,block代碼傳值
block 代碼塊 也稱作閉包 與C語言函數類似 具有反向傳值、回調的功能
block公式分兩種:
①聲明和實現寫到一塊的公式
傳回值類型(^block名字)(參數列表 參數類型 參數名) = ^(參數列表 參數類型 參數名){
實現代碼(如果有傳回值 需要return 傳回值類型的值)
};
調用:block名字(參數);
②聲明和實現分開寫的公式
(1)聲明
傳回值類型(^block名字)(參數列表);
(2)實現
block名字 = ^(參數列表){
實現代碼(如果有傳回值 需要return 傳回值類型的值)
};
(3)調用
block名字(實參);
注⃝ 在調用之前必須有實現的方法
今天在這裡主要將一下block的方向傳值(方向傳值就是在回調的時候給他一個值),通過button的樣本來講一下block怎麼反向傳值。
具體代碼如下:
在AppDelegate.m中建立帶導覽列的視窗
#import "AppDelegate.h"#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; [self.window makeKeyAndVisible]; return YES; } @end
在ViewController.m檔案裡面建立兩個button,並且建立一個繼承於UIViewController名叫BlockViewController類,匯入ViewController.m檔案裡面
#import "ViewController.h"#import "BlockViewController.h" @interface ViewController (){ UIButton *testButton; }@end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self createButton]; } - (void)createButton{ //放圖片的button testButton = [UIButton buttonWithType:UIButtonTypeCustom]; testButton.frame = CGRectMake(150, 100, 100, 100); testButton.backgroundColor = [UIColor brownColor]; testButton.layer.cornerRadius = 50; testButton.layer.masksToBounds = YES; [self.view addSubview:testButton]; //用於點擊的按鈕 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 250, 200, 50); [button setTitle:@"進入下一頁" forState:UIControlStateNormal]; button.backgroundColor = [UIColor brownColor]; button.layer.cornerRadius = 6; [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } //按鈕的觸發事件 - (void)clickButton{ BlockViewController *block = [[BlockViewController alloc]init]; //block的實現 block.buttonBlock = ^(UIImage *image){ [testButton setImage:image forState:UIControlStateNormal]; }; [self presentViewController:block animated:YES completion:nil]; } @end
在自己建立的BlockViewController.h檔案中聲明block的方法並且把block當屬性使用
#import <UIKit/UIKit.h>@interface BlockViewController : UIViewController//block的聲明 這裡把block當做屬性@property(nonatomic,copy)void(^buttonBlock)(UIImage *image);@end
在BlockViewController.m檔案中實現block的回調
#import "BlockViewController.h"@interface BlockViewController (){ UIButton *testButton;}@end @implementation BlockViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self backButton];}- (void)backButton{ testButton = [UIButton buttonWithType:UIButtonTypeCustom]; testButton.frame = CGRectMake(150, 100, 100, 100); [testButton setImage:[UIImage imageNamed:@"頭像3.jpg"] forState:UIControlStateNormal]; testButton.backgroundColor = [UIColor brownColor]; testButton.layer.cornerRadius = 50; [self.view addSubview:testButton]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 250, 200, 50); [button setTitle:@"返回上一頁" forState:UIControlStateNormal]; button.backgroundColor = [UIColor brownColor]; button.layer.cornerRadius = 6; [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];}- (void)clickButton{ //block的回調 self.buttonBlock([UIImage imageNamed:@"頭像3.jpg"]); //返回到上一頁 [self dismissViewControllerAnimated:YES completion:nil];}@end