UITabBarItem 操作给出 NSException

UITabBarItem action giving NSException

我有一个带有 4 个 UITabBarItem 的自定义 UITabBar,如下所示。

以下是我的代码的相关部分:

class BaseItemViewController: UIViewController,UITabBarDelegate{

@IBOutlet var customTabBar: UITabBar!

在 viewDidLoad 中:

self.customTabBar.delegate = self

自定义函数:

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if item.tag == 0 {
            print("Tag one")
        } else if item.tag == 1 {
            print("Tag two")
        } else if item.tag == 2 {
            print("Tag three")
        } else if item.tag == 3 {
            print("Tag four")
        } 
    }

我需要在点击标签栏项目后执行一个操作。我已将标签添加到 UIBarButtonItems,但是当我 运行 应用程序时,appDelegate 文件中的以下行出现错误。

class AppDelegate: UIResponder, UIApplicationDelegate {

Thread 1: signal SIGABRT

控制台还会打印以下内容:

2017-03-03 18:25:41.318 xxxxxx[1667:706023] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key NearbyHotelsButton.' * First throw call stack: ( 0 CoreFoundation 0x010f8494 exceptionPreprocess + 180 1 libobjc.A.dylib
0x00bb9e02 objc_exception_throw + 50 2 CoreFoundation
0x010f80b1 -[NSException raise] + 17 3 Foundation
0x0084b7f8 -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282 4 Foundation 0x007a5e6a _NSSetUsingKeyValueSetter + 115 5 Foundation 0x007a5def -[NSObject(NSKeyValueCoding) setValue:forKey:] + 295 6
UIKit 0x01d1c931 -[UIViewController setValue:forKey:] + 85 7 Foundation
0x007da54b -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 384 8 UIKit 0x01f99a62 -[UIRuntimeOutletConnection connect] + 132 9 libobjc.A.dylib 0x00bce00c -[NSObject performSelector:] + 62 10 CoreFoundation
0x01021131 -[NSArray makeObjectsPerformSelector:] + 273 11 UIKit
0x01f980fc -[UINib instantiateWithOwner:options:] + 2102 12 UIKit
0x01d24380 -[UIViewController _loadViewFromNibNamed:bundle:] + 429 13 UIKit 0x01d24db8 -[UIViewController loadView] + 189 14 UIKit 0x01d251c4 -[UIViewController loadViewIfRequired] + 154 15 UIKit 0x01d2bcca -[UIViewController __viewWillAppear:] + 114 16 UIKit
0x01d4ee45 -[UIViewController(UIContainerViewControllerProtectedMethods) beginAppearanceTransition:animated:] + 202 17 UIKit
0x01d61258 -[UINavigationController _startCustomTransition:] + 1389 18 UIKit 0x01d7302d -[UINavigationController _startDeferredTransitionIfNeeded:] + 803 19 UIKit 0x01d7439e -[UINavigationController __viewWillLayoutSubviews] + 68 20 UIKit 0x01f594b7 -[UILayoutContainerView layoutSubviews] + 252 21 UIKit
0x01c253d4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 810 22 libobjc.A.dylib 0x00bce059 -[NSObject performSelector:withObject:] + 70 23 QuartzCore
0x07ac7096 -[CALayer layoutSublayers] + 144 24 QuartzCore
0x07aba8b6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388 25 QuartzCore 0x07aba71a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26 26 QuartzCore 0x07aacee7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317 27 QuartzCore 0x07ae1847 _ZN2CA11Transaction6commitEv + 561 28 QuartzCore 0x07ae30b8 _ZN2CA11Transaction17flush_transactionEv + 50 29 UIKit
0x01b86f55 _afterCACommitHandler + 197 30 CoreFoundation
0x0100a75e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 30 31 CoreFoundation 0x0100a6be __CFRunLoopDoObservers + 398 32 CoreFoundation 0x0100003c __CFRunLoopRun + 1340 33 CoreFoundation 0x00fff846 CFRunLoopRunSpecific + 470 34 CoreFoundation
0x00fff65b CFRunLoopRunInMode + 123 35 GraphicsServices
0x04308664 GSEventRunModal + 192 36 GraphicsServices
0x043084a1 GSEventRun + 104 37 UIKit
0x01b54eb9 UIApplicationMain + 160 38 Voyate
0x000d6681 main + 145 39 libdyld.dylib
0x04b20a25 start + 1 40 ???
0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

我使用 中的类似示例进行了尝试。为什么会出现这个错误?

这不是问题操作。这是从故事板加载 viewcontroller 的问题。您有 IBOutlet,名称为 NearbyHotelsButton。但是你viewcontroller没有这个属性。