0

我在框架中有一个带有相应 xib 的自定义视图。在界面生成器中,我将文件所有者和视图本身设置为我的UIView( TotalTokenCountView) 子类。我还在自定义视图中为两个标签设置了插座。但是,当我在应用程序中使用此自定义视图时,会出现NSUnknownKeyException异常。

我尝试将插座变量设置为公共,以确保它们可以连接到框架之外,但这也不起作用。

我在这里错过了什么吗?我需要做更多设置才能连接插座吗?

public class TotalTokenCountView: UIView {

    @IBOutlet public weak var tokenLabel: UILabel!
    @IBOutlet public weak var tokenCountLabel: UILabel!

    public override func awakeFromNib() {
        tokenCountLabel.textColor = UIColor.deepPurple
        tokenLabel.textColor = UIColor.lavenderGrayMedium
    }

    static func create() -> TotalTokenCountView {
        return mybundle.loadNibNamed("TotalTokenCountView", owner: self, options: nil)!.first! as! TotalTokenCountView
    }
}

创建自定义视图的代码:

public class TotalTokenCountBarButtonItem: UIBarButtonItem {

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        self.customView = TotalTokenCountView.create()
    }
}

IB截图: 网点

自定义类

例外:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyFrameworkTarget.TotalTokenCountView 0x104ed9580> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tokenCountLabel.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a0d31e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010550a031 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a0d30b9 -[NSException raise] + 9
    3   Foundation                          0x0000000104f2bb47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x000000010622fe8a -[UIRuntimeOutletConnection connect] + 109
    5   CoreFoundation                      0x000000010a075e8d -[NSArray makeObjectsPerformSelector:] + 317
    6   UIKit                               0x000000010622e834 -[UINib instantiateWithOwner:options:] + 1856
    7   MyFrameworkTarget                   0x0000000104ed73bf _T012MyFrameworkTarget19TotalTokenCountViewC6createACyFZ + 431
    8   MyFrameworkTarget                   0x0000000104ed817b _T012MyFrameworkTarget28TotalTokenCountBarButtonItemC10commonInit33_82940FA39F221CEB8A8645FA6323AFBFLLyyF + 43
    9   MyFrameworkTarget                   0x0000000104ed80bc _T012MyFrameworkTarget28TotalTokenCountBarButtonItemCACSgSo7NSCoderC5coder_tcfc + 124
    10  MyFrameworkTarget                   0x0000000104ed813f _T012MyFrameworkTarget28TotalTokenCountBarButtonItemCACSgSo7NSCoderC5coder_tcfcTo + 47
    11  UIKit                               0x000000010622f725 -[UIClassSwapper initWithCoder:] + 246
    12  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    13  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    14  UIKit                               0x000000010671219b -[UINavigationItem initWithCoder:] + 715
    15  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    16  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    17  UIKit                               0x0000000105f44db1 -[UIViewController initWithCoder:] + 407
    18  UIKit                               0x0000000105fac885 -[UITabBarController initWithCoder:] + 65
    19  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    20  UIKit                               0x0000000106481a2a UINibDecoderDecodeObjectForValue + 1118
    21  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    22  UIKit                               0x0000000105f451a0 -[UIViewController initWithCoder:] + 1414
    23  UIKit                               0x0000000105f74dcc -[UINavigationController initWithCoder:] + 65
    24  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    25  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    26  UIKit                               0x000000010622f3f1 -[UIRuntimeConnection initWithCoder:] + 178
    27  UIKit                               0x000000010648188c UINibDecoderDecodeObjectForValue + 704
    28  UIKit                               0x0000000106481a2a UINibDecoderDecodeObjectForValue + 1118
    29  UIKit                               0x00000001064815bf -[UINibDecoder decodeObjectForKey:] + 246
    30  UIKit                               0x000000010622e5e2 -[UINib instantiateWithOwner:options:] + 1262
    31  UIKit                               0x00000001066bb406 -[UIStoryboard instantiateViewControllerWithIdentifier:] + 181
    32  UIKit                               0x0000000105da5862 -[UIApplication _loadMainStoryboardFileNamed:bundle:] + 112
    33  UIKit                               0x0000000105da5d33 -[UIApplication _loadMainInterfaceFile] + 272
    34  UIKit                               0x0000000105da44b9 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1403
    35  UIKit                               0x0000000106166f7e __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
    36  UIKit                               0x0000000106539a39 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    37  UIKit                               0x0000000106166bba -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    38  UIKit                               0x00000001061673db -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
    39  UIKit                               0x0000000106ad8614 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    40  UIKit                               0x0000000106ad84ae -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    41  UIKit                               0x00000001067bc75d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    42  UIKit                               0x00000001069b74b7 _performActionsWithDelayForTransitionContext + 100
    43  UIKit                               0x00000001067bc627 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    44  UIKit                               0x00000001065390e0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    45  UIKit                               0x0000000105da2eac -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
    46  UIKit                               0x0000000106375bcb -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    47  FrontBoardServices                  0x000000010d5752f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
    48  FrontBoardServices                  0x000000010d57dcfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
    49  libdispatch.dylib                   0x00000001089c97ec _dispatch_client_callout + 8
    50  libdispatch.dylib                   0x00000001089cedb8 _dispatch_block_invoke_direct + 592
    51  FrontBoardServices                  0x000000010d5a9470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    52  FrontBoardServices                  0x000000010d5a912e -[FBSSerialQueue _performNext] + 439
    53  FrontBoardServices                  0x000000010d5a968e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    54  CoreFoundation                      0x000000010a075bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    55  CoreFoundation                      0x000000010a05a4af __CFRunLoopDoSources0 + 271
    56  CoreFoundation                      0x000000010a059a6f __CFRunLoopRun + 1263
    57  CoreFoundation                      0x000000010a05930b CFRunLoopRunSpecific + 635
    58  GraphicsServices                    0x000000010cdc9a73 GSEventRunModal + 62
    59  UIKit                               0x0000000105da6057 UIApplicationMain + 159
    60  PinataApp                           0x0000000104be62c7 main + 55
    61  libdyld.dylib                       0x0000000108a46955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
4

2 回答 2

1

从 中删除引用(出口)File's Owner并将它们添加到TotalTokenCountView.

于 2018-07-02T17:02:59.803 回答
1

不确定这是否会导致您的问题,但在您调用 loadNibNamed 时,您将 self 作为所有者传递。由于您使用的是静态方法,因此这是对类类型的引用,而不是实例。

于 2018-07-02T17:09:15.747 回答