我正在开发IOS应用程序。我使用Facebook AsyncDisplayKit库。我想要一个ASNodeCell Bu中的按钮,当我被“”捕获时,“变量'node'未初始化。如何在ASNodeCell中添加UIButton或UIWebView控件。请帮助我
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用作按钮,因为它继承自ASControlNode(ASImageNode也是如此)。在指南第一页的底部http://asyncdisplaykit.org/guide/对此进行了描述。这也将允许您在后台线程而不是主线程上进行文本大小调整(在示例中提供的块在主队列上执行)。
为了完整起见,我还将评论您提供的代码。
您在创建块时尝试设置节点的框架,因此在初始化期间尝试在其上设置框架。那会导致您的问题。我认为使用initWithViewBlock时实际上不需要在节点上设置框架:因为在内部ASDisplayNode使用该块直接创建其_view属性,该属性最终添加到视图层次结构中。
我还注意到您正在从后台队列中调用addSubview:在调用该方法之前,应始终分派回主队列。为了方便起见,AsyncDisplayKit还向UIView添加addSubNode:。
尽管我建议您在此处使用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];
);
});
关于ios - 使用AsyncDisplayKit添加自定义按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28831366/