UITabBarItem动作给出NSException

时间:2017-03-03 13:03:36

标签: ios swift uitabbar uitabbaritem

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

enter image description here

enter image description here

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

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 {

  

线程1:信号SIGABRT

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

  

2017-03-03 18:25:41.318 xxxxxx [1667:706023] *终止应用程序   未被捕获的异常' NSUnknownKeyException',原因:   ' [   setValue:forUndefinedKey:]:此类不是键值   编码兼容关键的NearbyHotelsButton。'   * 第一次抛出调用堆栈:(0 CoreFoundation 0x010f8494 exceptionPreprocess + 180 1 libobjc.A.dylib
  0x00bb9e02 objc_exception_throw + 50 2 CoreFoundation
  0x010f80b1 - [NSException raise] + 17 3基金会   0x0084b7f8 - [NSObject(NSKeyValueCoding)setValue:forUndefinedKey:] +   282 4基金会0x007a5e6a   _NSSetUsingKeyValueSetter + 115 5基础0x007a5def - [NSObject(NSKeyValueCoding)setValue:forKey:] + 295 6
  UIKit 0x01d1c931 - [UIViewController   setValue:forKey:] + 85 7基础
  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图形服务
  0x04308664 GSEventRunModal + 192 36 GraphicsServices
  0x043084a1 GSEventRun + 104 37 UIKit
  0x01b54eb9 UIApplicationMain + 160 38 Voyate
  0x000d6681 main + 145 39 libdyld.dylib
  0x04b20a25开始+ 1 40 ???   0x00000001 0x0 + 1)libc ++ abi.dylib:以未捕获终止   NSException类型的异常(lldb)

我使用this question中的类似示例尝试了此操作。为什么会出现这个错误?

1 个答案:

答案 0 :(得分:2)

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