简体   繁体   English

无法继承UILabel:发送到UILabel的无法识别的选择器

[英]Unable to subclass UILabel: Unrecognized selector sent to UILabel

I'm using CocoaPods to import some things. 我正在使用CocoaPods导入一些东西。 Right now I'm trying to use TTTAttributedLabel , which is a rich subclass of UILabel , allowing for URL-clicking etc. 现在我正在尝试使用TTTAttributedLabel ,它是UILabel一个丰富的子类,允许URL点击等。

Now, I'm having trouble getting my app to recognise that my labels are actually instances of TTTAttributedLabel , and not regular UILabel 's. 现在,我无法让我的应用程序识别我的标签实际上是TTTAttributedLabel实例,而不是常规的UILabel

In my .xib, I have dragged a regular UILabel into my view, then edited the class of the label like this: 在我的.xib中,我将常规UILabel拖入我的视图中,然后编辑标签的类,如下所示:

xib类

It did autocomplete the name for me, so I know it knows about the class. 它确实为我自动完成了这个名字,所以我知道它知道这个课程。 Then, I have dragged a connection from the xib to the view's class, like this: 然后,我将xib中的连接拖到视图的类中,如下所示:

连接 It specifies itself as a TTTAttributedLabel automatically, because it recognises that that's what it is, or else it would've said UILabel . 它自动将自己指定为TTTAttributedLabel ,因为它识别它就是它,或者它会说UILabel

Since I've specified use_frameworks! 因为我已经指定了use_frameworks! in my podfile , I have to import the class, so in the top of my view's class, I have import TTTAttributedLabel . 在我的podfile ,我必须导入该类,因此在我视图的顶部类中,我import TTTAttributedLabel

Now, when I try to use the label, it autocompletes likes this: 现在,当我尝试使用标签时,它自动完成就像这样: 自动完成 With the correct class, TTTAttributedLabel . 使用正确的类, TTTAttributedLabel But when I do this: 但是当我这样做时:

print("labBody: ", labBody)

it prints out this: 它打印出来:

labBody:  <UILabel: 0x7fa754c86710; frame = (16 459; 42 21); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60000029cf20>>

thinking it's a UILabel. 认为这是一个UILabel。 If I try to access any of the TTTAttributedLabel -specific variables or functions, it still autocompletes them for me: 如果我尝试访问任何TTTAttributedLabel特定的变量或函数,它仍然会为我自动填充它们:

labBody.enabledTextCheckingTypes = NSTextCheckingResult.CheckingType.link.rawValue

But when using these on runtime, the entire app crashes, saying this: 但是在运行时使用这些时,整个应用程序崩溃了,这样说:

 -[UILabel setEnabledTextCheckingTypes:]: unrecognized selector sent to instance 0x7fdfc7c59300

Am I blind? 我瞎了吗? Why is this not working? 为什么这不起作用? This is what I've always been doing. 这就是我一直在做的事情。 I might be having a hard Tuesday.. I have tried all the regular stuff, cleaning, deleting derived data, restarting Xcode, deleting the app from the device before running again.. 我可能周二很辛苦..我已经尝试了所有常规的东西,清理,删除派生数据,重新启动Xcode,在再次运行之前从设备中删除应用程序..

I have even used TTTAttributedLabel before, I know it works. 我以前甚至使用过TTTAttributedLabel ,我知道它有效。 The only thing that's new is that this is my first time using use_frameworks! 唯一的新功能是这是我第一次使用use_frameworks! .. Does that affect the .xib? ..这会影响.xib吗? Making it unable to know about the subclass, or something..? 让它无法知道子类,或什么......?

I have no idea exactly why this worked, but I assume it has something to do with this pod being in Objective-C and something about binding and loading Obj-C stuff.. 我不知道为什么这会有效,但我认为它与Objective-C中的pod有关,还有一些关于绑定和加载Obj-C的东西..

Anyway, all I had to do was to wrap the object in a new local class. 无论如何,我所要做的就是将对象包装在一个新的本地类中。 的NewClass and then change the class of my labels in the .xib-file to this class instead of TTTAttributedLabel (and the outlets in my view's class, obviously). 然后将TTTAttributedLabel文件中的标签类更改为此类而不是TTTAttributedLabel (显然是我视图类中的出口)。

No need to do anything else than creating this class, it doesn't need to contain anything. 除了创建这个类之外,不需要做任何事情,它不需要包含任何东西。

I did read something about setting a flag, like a load ObjC -flag, I didn't try it. 我确实读过一些关于设置标志的内容,比如load ObjC -flag,我没试过。 I thought that that solution might also load a lot of other Object-C stuff I didn't need/want to load.. 我认为该解决方案可能还会加载很多其他我不需要/想要加载的Object-C东西..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UILabel无法识别的选择器已发送到实例 - UILabel unrecognized selector sent to instance 无法识别的选择器已发送到UILabel上的实例 - Unrecognized selector sent to instance On UILabel 无法识别的选择器已发送到实例(DTCoreText + UILabel) - unrecognized selector sent to instance (DTCoreText+UILabel) [UILabel copyWithZone:]: 无法识别的选择器发送到实例 - [UILabel copyWithZone:]: unrecognized selector sent to instance [UILabel numberOfComponentsInPickerView:]:无法识别的选择器已发送到实例…] - [UILabel numberOfComponentsInPickerView:]: unrecognized selector sent to instance …] [UILabel copyWithZone:]:无法识别的选择器 - [UILabel copyWithZone:]: unrecognized selector 无法识别的选择器发送到实例-尝试在视图中检查uilabel文本值 - unrecognized selector sent to instance - trying to check a uilabel text value in a view “无法识别的选择器发送到实例”设置UILabel的文本时 - “unrecognized selector sent to instance” When setting a UILabel's text iOS-ZBarSDK-应用程序崩溃,出现-[UILabel length]:无法识别的选择器发送到实例 - IOS - ZBarSDK - application crashes with -[UILabel length]: unrecognized selector sent to instance UILabel [__ NSCFString set]:无法识别的选择器发送到实例崩溃 - UILabel [__NSCFString set]: unrecognized selector sent to instance crash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM