I have a custom UITabBar with 4 UITabBarItems as given below.
Here are the relevant parts of my code:
class BaseItemViewController: UIViewController,UITabBarDelegate{
@IBOutlet var customTabBar: UITabBar!
in viewDidLoad:
self.customTabBar.delegate = self
custom function:
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")
}
}
I need to do an action once I click on the tab bar item. I have added tags to the UIBarButtonItems but when I run the app, there is an error in the following line in the appDelegate file.
class AppDelegate: UIResponder, UIApplicationDelegate {
saying
Thread 1: signal SIGABRT
Also the console prints the following:
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)
I tried this using a similar example in this question. Why is this error coming?
This is not problem action. This is problem loading viewcontroller from storyboard. You have IBOutlet with name NearbyHotelsButton. But you viewcontroller does not have this property.