我正在尝试使用纯代码在viewController之间创建UI练习块传递值。但回调块无法正常工作。 NSLog方法没有在调试区域上打印任何内容。这是代码。给我一些提示,谢谢。
VC.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (copy, nonatomic) void (^callBack)(NSString *text);
@end
VC.m
- (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;
};
};
}
答案 0 :(得分:0)
唯一的方法是你的财产
@property (copy, nonatomic) void (^callBack)(NSString *text);
是空的。尝试在buttonAction
方法中放置断点并查看属性。
答案 1 :(得分:0)
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;
}