iOS Objective-C块回调问题
我想用纯代码在viewController之间创建UI练习块传递值。但回调块不起作用。 NSLog方法没有打印任何东西在调试区域。这是代码。给我一些提示,谢谢。iOS Objective-C块回调问题
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;
};
};
}
唯一的办法是你财产
@property (copy, nonatomic) void (^callBack)(NSString *text);
是空的。尝试在buttonAction
方法中放置断点并查看属性。
由于桑德和克里希纳CA提到你的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;
}
对你的步骤NSLog(@“Second view controller's button tapped!”);没有工作,为什么 - – Haixiao
我看着你的更新代码,你每次在VC2.m中分配一个新的回调viewWillAppear,这意味着你分配给第一个视图控制器的回调将被覆盖。如果你把NSLog(@“Second view controller's button tapped!”);在你的第二个视图控制器的viewWillAppear的callBack中它应该打印该消息。顺便说一句,你也错过了[super viewWillAppear];在你的VC2中。m –
我已经阅读了一些关于block的书,并且已经完成了它,任何方式thx为您的帮助。 – Haixiao
您的'callBack'是无开始。我建议你看看这个例子来更多地了解Objective-C块。 http://www.appcoda.com/objective-c-blocks-tutorial/ – KrishnaCA
thx为你的链接,我已修复它。你有任何其他块和快速关闭教程链接或视频? – Haixiao
我在第二个viewDidLoad中初始化回调块。secondViewController工作,但我不能从firstViewController调用它,如何在firstVC中调用它。 – Haixiao