目標:在兩個獨立的控制器的界面之間進行反向傳值
關鍵技術:block
代碼編寫及運行環境:Xcode6.4 / 模擬器8.4
語言:Objective-C
注:使用純代碼實現,不使用xib/storyboard
效果圖:



前期注意:

代碼實現如下:
1.

1 // 2 // AppDelegate.h 3 // blockPassValue 4 // 5 // Created by xiaoC on 16/9/28. 6 // Copyright (c) 2016年 xiaoLeC. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface AppDelegate : UIResponder <UIApplicationDelegate> 12 13 @property (strong, nonatomic) UIWindow *window; 14 15 16 @end
1 // 2 // AppDelegate.m 3 // blockPassValue 4 // 5 // Created by xiaoC on 16/9/28. 6 // Copyright (c) 2016年 xiaoLeC. All rights reserved. 7 // 8 9 #import "AppDelegate.h" 10 #import "ViewController.h" 11 12 @interface AppDelegate () 13 14 @end 15 16 @implementation AppDelegate 17 18 19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 // Override point for customization after application launch. 21 22 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 23 self.window.backgroundColor = [UIColor whiteColor]; 24 [self.window makeKeyAndVisible]; 25 26 self.window.rootViewController = [[ViewController alloc] init]; 27 28 return YES; 29 } 30 31 - (void)applicationWillResignActive:(UIApplication *)application { 32 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 33 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 34 } 35 36 - (void)applicationDidEnterBackground:(UIApplication *)application { 37 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 38 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 39 } 40 41 - (void)applicationWillEnterForeground:(UIApplication *)application { 42 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 43 } 44 45 - (void)applicationDidBecomeActive:(UIApplication *)application { 46 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 47 } 48 49 - (void)applicationWillTerminate:(UIApplication *)application { 50 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 51 } 52 53 @end
2.

1 // 2 // ViewController.h 3 // blockPassValue 4 // 5 // Created by xiaoC on 16/9/28. 6 // Copyright (c) 2016年 xiaoLeC. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface ViewController : UIViewController 12 13 14 @end
1 // 2 // ViewController.m 3 // blockPassValue 4 // 5 // Created by xiaoC on 16/9/28. 6 // Copyright (c) 2016年 xiaoLeC. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "YFSecondViewController.h" 11 12 @interface ViewController () 13 @property(nonatomic,strong)UILabel *lab; 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view, typically from a nib. 21 22 self.view.backgroundColor = [UIColor grayColor]; 23 24 //添加一個跳轉的按鈕 25 UIButton *jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 26 jumpBtn.frame = CGRectMake(100, 200, 200, 50); 27 jumpBtn.backgroundColor = [UIColor yellowColor]; 28 [jumpBtn setTitle:@"跳轉" forState:UIControlStateNormal]; 29 [jumpBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; 30 [jumpBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside]; 31 [self.view addSubview:jumpBtn]; 32 33 //創建一個label,接收傳回來的值 34 _lab = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 200, 50)]; 35 _lab.backgroundColor = [UIColor yellowColor]; 36 _lab.textAlignment = NSTextAlignmentCenter; 37 [self.view addSubview:_lab]; 38 } 39 40 //調用該方法跳轉到YFSecondViewController控制器界面 41 -(void)jump 42 { 43 YFSecondViewController *yfSecondV = [[YFSecondViewController alloc] init]; 44 [self presentViewController:yfSecondV animated:YES completion:nil]; 45 46 //block代碼塊 47 yfSecondV.block = ^void (NSString *str){ 48 49 //給label設置返回來的值 50 _lab.text = str; 51 }; 52 } 53 54 55 56 @end
3.

1 // 2 // YFSecondViewController.h 3 // 4 // 5 // Created by xiaoLeC on 16/9/28. 6 // 7 // 8 9 #import <UIKit/UIKit.h> 10 11 typedef void(^myBlock)(NSString *str); 12 13 @interface YFSecondViewController : UIViewController 14 15 @property (nonatomic,copy) myBlock block; 16 17 @end
1 // 2 // YFSecondViewController.m 3 // 4 // 5 // Created by xiaoLeC on 16/9/28. 6 // 7 // 8 9 #import "YFSecondViewController.h" 10 11 @interface YFSecondViewController () 12 13 @end 14 15 @implementation YFSecondViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 self.view.backgroundColor = [UIColor orangeColor]; 20 21 //來一些數據 22 NSArray *arr = @[@"666",@"333",@"我是90后"]; 23 for (int i=0; i<3; i++) { 24 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 25 btn.frame = CGRectMake(100, (i*50)+80, 100, 40); 26 [btn setTitle:arr[i] forState:UIControlStateNormal]; 27 [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; 28 [btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; 29 [self.view addSubview:btn]; 30 } 31 32 33 } 34 35 -(void)back:(UIButton *)sender 36 { 37 //調用block,要在銷毀控制器之前調用block 38 self.block(sender.currentTitle); 39 40 //銷毀控制器 41 [self dismissViewControllerAnimated:YES completion:nil]; 42 } 43 44 - (void)didReceiveMemoryWarning { 45 [super didReceiveMemoryWarning]; 46 // Dispose of any resources that can be recreated. 47 } 48 49 50 @end
/*
block的回調的使用步驟
1.聲明 : 在誰那里調用就在誰那里聲明
實現代碼
typedef void(^MyBlock)(NSString *name);//block的重命名
@property (nonatomic,copy) MyBlock block;//block的聲明
2.實現 : 誰要裝值就在誰那里實現
實現代碼
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];//在這里沒用導航控制器,用presentViewController來進入下一個視圖
//block實現
secondVC.block = ^void(NSString *name)
{
_label.text = name;
};//block的位置擺放很作用的,因為它是一個函數,不過一定不能放在使用它的對象的外面和前面就好了
3.調用 : 誰要傳值就在誰那里調用
self.block(@"呵呵");//block的調用
總結一句話:block用在不同視圖控制器之間的值回傳,回傳還有代理、單例,在回傳中最簡單的就是用block了
*/
