使用Block在A、B頁面實現傳值
Block使用起來很方便,不過這里沒有將其作為方法的一個參數,而是簡單的作為一個屬性來使用。
思路如下:
在B頁面
1.定義一個Block別名
//別名(申明) 類型 ----> void (^) (NSString *text)typedef
void(^ChangeTextBlock) (NSString *text);
2.定義一個block
@property (nonatomic, copy) ChangeTextBlock block;
3.在B頁面的Button事件響應方法里面修改值
//修改值 -----> 調用block來修改值
if (self.block != nil) {
self.block(textField.text);
}
4.將值傳到A頁面
//1. 實現block,使用__block的原因:是防止內存的泄露__block
RootViewController *rootVC = self;
//2. block的初始化
_modalViewController.block = ^(NSString *text) {
//如果不用__block的對象,那么這里會提示一個關于內存的警告
UILabel *label = (UILabel *) [rootVC.view viewWithTag:1000];
label.text = text;
};
完整代碼:
A頁面
.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
.m文件
#import "RootViewController.h"
#import "ModalViewController.h"
@interface RootViewController () {
ModalViewController *_modalViewController;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1) 設置背景顏色
self.view.backgroundColor = [UIColor cyanColor];
//2) 設置一個Label
//a) 創建一個Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 100, 270, 30)];
//b) 設置該label的tag label.tag = 1000;
//c) 設置label的內容
label.text = @"Block的傳值";
//d) 設置背景顏色
label.backgroundColor = [UIColor orangeColor];
//e) 設置字體顏色
label.textColor = [UIColor whiteColor];
//f) 設置居中方式
label.textAlignment = NSTextAlignmentCenter;
//g) 添加label
[self.view addSubview:label];
//3) 設置跳轉你的button
//a) 創建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//b) 設置其frame
button.frame = CGRectMake(0, 0, 200, 30);
//c) 設置其在屏幕的中心
button.center = self.view.center;
//d) 設置背景顏色
button.backgroundColor = [UIColor lightGrayColor];
//e) 設置顯示的內容
[button setTitle:@"跳轉" forState:UIControlStateNormal];
//f) 設置相應事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//g) 添加到頁面上
[self.view addSubview:button];
//4) 初始化模態視圖
_modalViewController = [[ModalViewController alloc] init];
}
#pragma mark - 點擊的按鍵響應
- (void) buttonAction: (UIButton *) button {
//1. 實現block,使用__block的原因:是防止內存的泄露 __block
RootViewController *rootVC = self;
//2. block的初始化
_modalViewController.block = ^(NSString *text) {
//如果不用__block的對象,那么這里會提示一個警告 ------> 內存的
UILabel *label = (UILabel *) [rootVC.view viewWithTag:1000];
label.text = text;
};
//3. 彈窗模式
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//4. 模態視圖
[self presentViewController:_modalViewController animated:YES completion:nil];
}
@end
B頁面
.h文件
#import <UIKit/UIKit.h>
//這里要定義一個block的別名(申明) 類型 ----> void (^) (NSString *text)
typedef void(^ChangeTextBlock) (NSString *text);
@interface ModalViewController : UIViewController
//定義一個block
@property (nonatomic, copy) ChangeTextBlock block;
@end
.m文件
#import "ModalViewController.h"
@interface ModalViewController ()
@end
@implementation ModalViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1) 設置背景顏色
self.view.backgroundColor = [UIColor orangeColor];
//2) 設置一個TextField
//a) 創建一個TextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(25, 100, 270, 30)];
//b) 設置tag
textField.tag = 2000;
//c) 設置顯示的效果
textField.borderStyle = UITextBorderStyleRoundedRect;
//d) 顯示提示語
textField.placeholder = @"請輸入一段文字...";
//e) 添加到self.view上
[self.view addSubview:textField];
//3) 設置跳轉你的button
//a) 創建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//b) 設置其frame
button.frame = CGRectMake(0, 0, 200, 30);
//c) 設置其在屏幕的中心
button.center = self.view.center;
//d) 設置背景顏色
button.backgroundColor = [UIColor lightGrayColor];
//e) 設置顯示的內容
[button setTitle:@"返 回" forState:UIControlStateNormal];
//f) 設置相應事件
[button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
//g) 添加到頁面上
[self.view addSubview:button];
}
- (void) backAction: (UIButton *) button {
//1) 取值
UITextField *textField = (UITextField *)[self.view viewWithTag:2000];
//2) 修改值 -----》 調用block來修改值
if (self.block != nil) {
self.block(textField.text);
}
//3) 關閉模態視圖
[self dismissViewControllerAnimated:YES completion:nil];
}
@end