[英]iOS Objective-C block callback issue
我正在嘗試使用純代碼在viewController之間創建UI練習塊傳遞值。 但是回調塊不起作用。 NSLog方法未在調試區域上打印任何內容。 這是代碼。 給我一些提示,謝謝。
VC文件
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (copy, nonatomic) void (^callBack)(NSString *text);
@end
VC
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_textField.backgroundColor = [UIColor whiteColor];
}
return _textField;
}
- (UIButton *)button {
if (!_button) {
_button = [[UIButton alloc] init];
_button.backgroundColor = [UIColor blueColor];
[_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _button;
}
- (void)setupUI {
[self.view addSubview:self.textField];
[self.view addSubview:self.button];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(200);
make.height.mas_equalTo(50);
make.centerX.mas_equalTo(self.view.mas_centerX);
make.centerY.mas_equalTo(self.view);
}];
[self.button mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(200);
make.height.mas_equalTo(50);
make.centerX.mas_equalTo(self.view);
make.centerY.mas_equalTo(self.view).offset(100);
}];
}
- (void)buttonAction {
NSString *str = self.textField.text;
if (self.callBack != nil) {
self.callBack(str);
NSLog(@"This statement didnt print in log");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
更新代碼
VC2.m
- (void)viewWillAppear:(BOOL)animated{
self.callBack = ^(NSString *text){
};
}
- (void)buttonAction {
if (self.callBack) {
NSLog(@"It worked on debug area %@", self.textField.text);
self.callBack(self.textField.text);
}
self.textField.text = @"";
}
VC1.m
- (void)viewDidLoad {
[super viewDidLoad];
_secondVc = [[SecondViewController alloc] init];
_secondVc.callBack = ^(NSString *str){
};
[self setupUI];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)viewWillAppear:(BOOL)animated {
if (_secondVc.callBack != nil) {
NSLog(@"It wrked on debug screen");
_secondVc.callBack = ^(NSString *str){
NSLog(@"It didn't worked on debug screen");
//I want set my label.text = str;
};
};
}
唯一的辦法就是你財產
@property (copy, nonatomic) void (^callBack)(NSString *text);
是空的。 嘗試在buttonAction
方法中放置斷點並查看屬性。
正如Sander和KrishnaCA提到的,您的callBack
叫為零。 我建議您像這樣創建塊的定義:
typedef void(^TextBlock)(NSString *text);
然后將您的屬性更改為:
@property (copy, nonatomic) TextBlock callBack;
在第一個視圖控制器中創建塊的副本:
@interface FirstViewController()
@property (copy, nonatomic) TextBlock firstViewControllerCallBack;
@end
初始化回調副本(即在viewDidLoad中)
- (void)viewDidLoad {
[super viewDidLoad];
self.firstViewControllerCallBack = ^(NSString *text){
NSLog(@"Second view controller's button tapped!");
};
}
在呈現/推動回調之前,將回調分配給第二個視圖控制器:
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.callBack = self.firstViewControllerCallBack; // Assign the callback
// ... Presenting the view controller
完成后,清理完成塊(即在viewWillDisappear中):
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.firstViewControllerCallBack = nil;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.