-1

I am just trying to print something when a button is clicked.

import UIKit

class ViewController: UIViewController {

    @IBAction func bu(_ sender: UIButton) {
        print("click")
    }

}

its show error in the console as

2017-05-22 18:28:58.751 will it work[24356:1074948] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.' * First throw call stack: ( 0 CoreFoundation 0x0000000110a06b0b exceptionPreprocess + 171 1 libobjc.A.dylib
0x000000010ddcb141 objc_exception_throw + 48 2 CoreFoundation
0x0000000110a06a59 -[NSException raise] + 9 3 Foundation
0x000000010d8e0e8b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292 4 UIKit 0x000000010e431644 -[UIViewController setValue:forKey:] + 87 5 UIKit 0x000000010e69e6b9 -[UIRuntimeOutletConnection connect] + 109 6
CoreFoundation 0x00000001109ace8d -[NSArray makeObjectsPerformSelector:] + 269 7 UIKit
0x000000010e69d06f -[UINib instantiateWithOwner:options:] + 1856 8
UIKit 0x000000010e437c73 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x000000010e438589 -[UIViewController loadView] + 177 10 UIKit
0x000000010e4388ba -[UIViewController loadViewIfRequired] + 195 11 UIKit 0x000000010e43910a -[UIViewController view] + 27 12 UIKit 0x000000010e30163a -[UIWindow addRootViewControllerViewIfPossible] + 65 13 UIKit 0x000000010e301d20 -[UIWindow _setHidden:forced:] + 294 14 UIKit 0x000000010e314b6e -[UIWindow makeKeyAndVisible] + 42 15 UIKit
0x000000010e28e31f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346 16 UIKit 0x000000010e294584 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709 17 UIKit 0x000000010e291793 -[UIApplication workspaceDidEndTransaction:] + 182 18 FrontBoardServices 0x00000001121065f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK
+ 24 19 FrontBoardServices 0x000000011210646d -[FBSSerialQueue _performNext] + 186 20 FrontBoardServices 0x00000001121067f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 21 CoreFoundation 0x00000001109acc01 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 22 CoreFoundation 0x00000001109920cf __CFRunLoopDoSources0 + 527 23 CoreFoundation 0x00000001109915ff __CFRunLoopRun + 911 24 CoreFoundation
0x0000000110991016 CFRunLoopRunSpecific + 406 25 UIKit
0x000000010e29002f -[UIApplication _run] + 468 26 UIKit
0x000000010e2960d4 UIApplicationMain + 159 27 will it work
0x000000010d638c17 main + 55 28 libdyld.dylib
0x000000011199665d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

I think button is not even connected here is the image:
enter link description here

Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

1

@Narasimha Reddy Please disconnect your button outlet from storyboard.

enter image description here

Ved Rauniyar
  • 1,539
  • 14
  • 21
0

Please disconnect your button outlet from storyboard.

Ved Rauniyar
  • 1,539
  • 14
  • 21
0

If you can run without the view connected and don't get an exception, something is wrong. When the UIViewController loads and you try to display it, it uses the view to know what to show. Double check your class and connections. If you don't find the problem, post more information and we'll try to figure it out.

KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

The most significant information is

'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.'

Looking at your code there is only an action bu rather than button which is most likely a typo.

So replace

@IBAction func bu(_ sender: UIButton)

with

@IBAction func button(_ sender: UIButton)
vadian
  • 274,689
  • 30
  • 353
  • 361