【问题标题】:tvOS UITableView causes error on Apple TV but not simulatortvOS UITableView 在 Apple TV 上导致错误但不是模拟器
【发布时间】:2015-11-11 19:34:15
【问题描述】:

我的控制器上有一个 UITableView。当控制器显示页面时,它处于焦点。

当焦点行发生变化时,我想在下面的代码中更新一个 UITextView(称为 _appText)。

这一切在模拟器中运行良好,但是当我在 Apple TV 上运行时出现以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用,原因:“_ignoreFocusUpdateIfNeeded 在焦点更新开始时不应为 YES。”

我在 Apple TV 上运行 tvOS 9.01 版

代码如下:

- (void)tableView:(UITableView *)tableView didUpdateFocusInContext(UITableViewFocusUpdateContext *)contextwithAnimationCoordinator:(UIFocusAnimationCoordinator*)coordinator{
NSInteger ourRow=context.nextFocusedIndexPath.row;

NSString *path =[[NSBundle mainBundle] pathForResource:[_appIconNames objectAtIndex:ourRow] ofType:@"html"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSAttributedString *thisString=[[NSAttributedString alloc] initWithData:[content dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}documentAttributes:nil error:nil];
_appText.attributedText=thisString;
}

导致错误的行是:

NSAttributedString *thisString=[[NSAttributedString alloc] initWithData:[content dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}documentAttributes:nil error:nil];

无法弄清楚为什么它在模拟器中有效,但在 Apple TV 本身中无效。

【问题讨论】:

  • 您是否使用计算机键盘在模拟器中导航?我在另一个项目中遇到了与您相同的错误,但只有当我在模拟器上使用“Apple TV Remote”进行导航时。我还不知道如何解决这个问题,但可能与此有关。
  • 另外,我遇​​到了这个问题,因为我也在使用 NSAttributedString 来显示 NSHTMLTextDocumentType 文档。这可能是与焦点和 UITextView 或 NSAttributedString 相关的错误
  • 我也遇到了这个问题。有什么更新吗?我不确定发生了什么,但只有当我试图将 HTML 放入像你这样的属性字符串时才会发生这种情况。

标签: objective-c uitableview tvos apple-tv


【解决方案1】:

试试这个:

目标-C:

dispatch_async(dispatch_get_main_queue(), ^{
   _appText.attributedText=thisString;
}

斯威夫特:

dispatch_async(dispatch_get_main_queue()) {
   _appText.attributedText = thisString
}

【讨论】:

    【解决方案2】:

    Swift 3 中:

    DispatchQueue.main.async {
       _appText.attributedText = thisString
    }
    

    【讨论】:

      【解决方案3】:

      我相信这就是您正在寻找的答案: 不知道为什么需要这样做,但我遇到了同样的问题,这确实解决了它...... 借用 Banana 的,真正的问题是字符串的转换,而不是赋值。

      dispatch_async(dispatch_get_main_queue(), ^{
          NSAttributedString *thisString=[[NSAttributedString alloc] 
          initWithData:[content dataUsingEncoding:NSUTF8StringEncoding] 
          options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
                    NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
          documentAttributes:nil error:nil];
      });
      

      【讨论】:

      • 这可能会回答这个问题,但如果您格式化代码以便无需横向滚动就可以阅读它,这将是一个更好的答案。很高兴看到整个答案没有任何横向滚动。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      相关资源
      最近更新 更多