ios - UITabBarItem 操作给出 NSException

标签 ios swift uitabbar uitabbaritem

我有一个带有 4 个 UITabBarItem 的自定义 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 {

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)

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

最佳答案

这不是问题操作。这是从 Storyboard 中加载 View Controller 的问题。您有名为 NearbyHotelsButtonIBOutlet。但是你的viewcontroller没有这个属性。

关于ios - UITabBarItem 操作给出 NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579635/

相关文章:

ios - XCTest无法读取启动参数

iphone - iOS5如何 "lazily"初始化标签栏 Controller ?

ios - 线程 1 : "-[_UIDatePickerIOSCompactView setHighlightsToday:]: unrecognized selector sent to instance"

ios - AirDrop 共享 - 自定义 URL 方案或网站 URL

ios - iOS Assets 的 PDF 图像

ios - 如何快速制作我自己的语法糖?

swift - 如何提示在 Swift 中将哪个重写函数作为参数传递?

ios - 防止UICollectionViewCell的跳转

ios - 重新初始化标签栏中的标签

iphone - 在 UITabBar View 中切换 View