【问题标题】:TTTAttributedLabel:Crash-tapping on link. [NSConcreteAttributedString reverseObjectEnumerator]: unrecognized selector sent to instance 0x8eb7f60'TTTAttributedLabel:点击链接时崩溃。 [NSConcreteAttributedString reverseObjectEnumerator]:无法识别的选择器发送到实例 0x8eb7f60'
【发布时间】:2014-10-08 11:57:45
【问题描述】:

正在开发聊天应用程序。在我的自定义单元格中使用 TTTAttributedLabel。这样我就可以检测链接。我在自定义单元格中实现了该代码。

    self.tttAttributedLabel=[[TTTAttributedLabel alloc] init];
    self.tttAttributedLabel.font = [UIFont systemFontOfSize:14.0];
    self.tttAttributedLabel.numberOfLines = 0;
    self.tttAttributedLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.tttAttributedLabel.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(__bridge NSString *)kCTUnderlineStyleAttributeName];
    self.tttAttributedLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink; 
    self.tttAttributedLabel.delegate = self;
    [self.contentView addSubview:self.tttAttributedLabel];

我在下面的函数中崩溃了。我不知道为什么会有崩溃。似乎 self.links 不是一个有效的数组。

  - (NSTextCheckingResult *)linkAtCharacterIndex:(CFIndex)idx
{
     NSEnumerator *enumerator = [self.links reverseObjectEnumerator]; //crash on this 
   NSTextCheckingResult *result = nil;
   while ((result = [enumerator nextObject])) {
    if (NSLocationInRange((NSUInteger)idx, result.range)) {
        return result;
    }
}
return nil;
}

【问题讨论】:

  • 我会在- (void)setLinks:(NSArray *)links 处设置一个断点,然后进行回溯以查看之前调用的内容。从那里开始工作,直到您弄清楚 self.links 的设置位置。
  • 您找到解决方案了吗?我遇到了同样的问题,链接 NSArray 似乎正在失去参考
  • @kevinl 不要在这里插入我自己的答案,但问题(正如我在下面提到的)是 init 不是正确的指定初始值设定项;你必须使用initWithFrame:。否则,链接属性将不会被初始化。
  • @JonathanHersh 谢谢乔纳森。但是,我一直在使用 initWithFrame。

标签: ios hyperlink uilabel nsattributedstring tttattributedlabel


【解决方案1】:

您应该使用指定的初始化程序initWithFrame:,即使您将CGRectZero 作为框架传入。您正在使用init,它不会执行初始化links 属性的常用初始化方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多