使用AsyncDisplayKit添加自定义button

我正在开发一个IOS应用程序。 我使用Facebook AsyncDisplayKit库。 我想在ASNodeCell中的一个button我得到了“ variables”节点是未被初始化当被块捕获如何添加UIButton或UIWebView控制在ASNodeCell请帮助我

dispatch_queue_t _backgroundContentFetchingQueue; _backgroundContentFetchingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); dispatch_async(_backgroundContentFetchingQueue, ^{ ASDisplayNode *node = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{ UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button sizeToFit]; node.frame = button.frame; return button; }]; // Use `node` as you normally would... node.backgroundColor = [UIColor redColor]; [self.view addSubview:node.view]; }); 

在这里输入图像说明

注意在你的情况下,不需要使用UIButton,你可以使用ASTextNode作为一个button,因为它从ASControlNodeinheritance(ASImageNode也一样)。 这在指南第一页的底部进行了描述: http : //asyncdisplaykit.org/guide/ 。 这也将允许您在后台线程而不是主线程(在您的示例中提供的块在主队列上执行)进行文本大小调整。

为了完整起见,我还会对您提供的代码发表评论。

在创build时,您正在尝试在块中设置节点的框架,所以您在尝试在其初始化过程中设置框架。 这会导致你的问题。 当你使用initWithViewBlock时,我不认为你真的需要在节点上设置框架:因为内部ASDisplayNode使用块来直接创build它的_view属性,最后添加到视图层次结构中。

我也注意到你正在调用addSubview:从后台队列中,你应该总是调用回主队列,然后再调用该方法。 为了方便,AsyncDisplayKit还添加了addSubNode:到UIView。

我已经改变了你的代码来反映改变,但我build议你在这里使用ASTextNode。

 dispatch_queue_t _backgroundContentFetchingQueue; _backgroundContentFetchingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); dispatch_async(_backgroundContentFetchingQueue, ^{ ASDisplayNode *node = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{ UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button sizeToFit]; //node.frame = button.frame; <-- this caused the problem return button; }]; // Use `node` as you normally would... node.backgroundColor = [UIColor redColor]; // dispatch to main queue to add to view dispatch_async(dispatch_get_main_queue(), [self.view addSubview:node.view]; // or use [self.view addSubnode:node]; ); });