Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Objective-C块回调问题_Ios_Objective C_Callback_Objective C Blocks - Fatal编程技术网

iOS Objective-C块回调问题

iOS Objective-C块回调问题,ios,objective-c,callback,objective-c-blocks,Ios,Objective C,Callback,Objective C Blocks,我试图使用纯代码在viewController之间创建UI实践块传递值。但是回调块不起作用。NSLog方法没有在调试区域打印任何内容。这是密码。给我一些建议,谢谢 VC.h 更新代码 VC2.m VC1.m }唯一的办法是你拥有自己的财产 @property (copy, nonatomic) void (^callBack)(NSString *text); 是空的。尝试将断点放入按钮操作方法中,并查看属性。正如Sander和KrishnaCA提到的那样,您的回调为零。我建议您创建块的定义

我试图使用纯代码在viewController之间创建UI实践块传递值。但是回调块不起作用。NSLog方法没有在调试区域打印任何内容。这是密码。给我一些建议,谢谢

VC.h


更新代码

VC2.m

VC1.m


}

唯一的办法是你拥有自己的财产

@property (copy, nonatomic) void (^callBack)(NSString *text);

是空的。尝试将断点放入
按钮操作
方法中,并查看属性。

正如Sander和KrishnaCA提到的那样,您的
回调
为零。我建议您创建块的定义,如下所示:

typedef void(^TextBlock)(NSString *text);
然后将您的属性更改为:

@property (copy, nonatomic) TextBlock callBack;
在第一个视图控制器中创建块的副本:

@interface FirstViewController()

@property (copy, nonatomic) TextBlock firstViewControllerCallBack;

@end
初始化回调副本(即在viewDidLoad中)

在显示/推送回调之前,将其分配给第二个视图控制器:

SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.callBack = self.firstViewControllerCallBack; // Assign the callback
// ... Presenting the view controller
完成后清理完成块(即,在视图中将消失):


您的
回调开始时为零。我建议您查看这个示例,以了解更多有关objective-C块的信息。thx对于你的链接,我已经修复了。你还有其他block和swift closure教程链接或视频吗?我在second ViewDiLoad中初始化回调块。second ViewController工作,但我无法从firstViewController调用它。如何在firstVC中调用它。休整你的步骤NSLog(@“second view controller的按钮已点击!”);没有工作,为什么--我查看了您的更新代码,每次Viewwills出现在VC2.m中时,您都会分配一个新的回调,这意味着您分配给第一个视图控制器的回调将被覆盖。如果您将NSLog(@“点击第二视图控制器的按钮!”);在第二个视图控制器的VIEWWILLEXPEND的回调中,它应该打印消息。顺便说一句,您还缺少[超级视图将出现];在您的VC2.mI中,您已经阅读了一些关于block的书籍,并且已经完成了block,无论如何,thx都会为您提供帮助。
typedef void(^TextBlock)(NSString *text);
@property (copy, nonatomic) TextBlock callBack;
@interface FirstViewController()

@property (copy, nonatomic) TextBlock firstViewControllerCallBack;

@end
- (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
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.firstViewControllerCallBack = nil;
}