system21
system21

Reputation: 361

UIButton Causing Crash

I have some UIButtons and when i load that ViewCOntroller the app crashs and when I remove the UIbuttons the app works and loads

This is what happens

2016-01-17 16:27:51.151 HHKNBK[36147:32655609] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<HHKNBK.ViewController 0x7fed73052380> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key one.'

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f757e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001116bfdeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f757aa9 -[NSException raise] + 9
    3   Foundation                          0x000000010fd489bb -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
    4   UIKit                               0x000000011032b320 -[UIViewController setValue:forKey:] + 88
    5   UIKit                               0x0000000110559f41 -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x000000010f6984a0 -[NSArray makeObjectsPerformSelector:] + 224
    7   UIKit                               0x0000000110558924 -[UINib instantiateWithOwner:options:] + 1864
    8   UIKit                               0x0000000110331eea -[UIViewController _loadViewFromNibNamed:bundle:] + 381
    9   UIKit                               0x0000000110332816 -[UIViewController loadView] + 178
    10  UIKit                               0x0000000110332b74 -[UIViewController loadViewIfRequired] + 138
    11  UIKit                               0x00000001103332e7 -[UIViewController view] + 27
    12  UIKit                               0x00000001103858b0 -[UINavigationController preferredContentSize] + 194
    13  UIKit                               0x000000011030a0aa -[UIPresentationController preferredContentSizeDidChangeForChildContentContainer:] + 59
    14  UIKit                               0x0000000110306438 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 95
    15  UIKit                               0x00000001101a84a2 _runAfterCACommitDeferredBlocks + 317
    16  UIKit                               0x00000001101bbc01 _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
    17  UIKit                               0x00000001101c7af3 _afterCACommitHandler + 90
    18  CoreFoundation                      0x000000010f683367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    19  CoreFoundation                      0x000000010f6832d7 __CFRunLoopDoObservers + 391
    20  CoreFoundation                      0x000000010f678f2b __CFRunLoopRun + 1147
    21  CoreFoundation                      0x000000010f678828 CFRunLoopRunSpecific + 488
    22  GraphicsServices                    0x000000011486cad2 GSEventRunModal + 161
    23  UIKit                               0x000000011019c610 UIApplicationMain + 171
    24  HHKNBK                              0x000000010f10677d main + 109
    25  libdyld.dylib                       0x00000001121d192d start + 1
    26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Here is the code

import UIKit

class ViewController: UIViewController {


    @IBOutlet var live: mainbutton!

    @IBOutlet var dt: mainbutton!

    @IBOutlet var ms: mainbutton!
    @IBOutlet var `as1`: mainbutton!
    @IBOutlet var read: mainbutton!
    @IBOutlet var pg: mainbutton!


    override func viewDidLoad() {
        super.viewDidLoad()


        self.view.backgroundColor = color125

        live.setTitle("Live", forState: UIControlState.Normal)
        dt.setTitle("Dhamma Talk", forState: UIControlState.Normal)
        ms.setTitle("Mediations Stream", forState: UIControlState.Normal)
        as1.setTitle("Adavanced And Special Streams", forState: UIControlState.Normal)
        read.setTitle("Read", forState: UIControlState.Normal)
        pg.setTitle("Program Guide", forState: UIControlState.Normal)


        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

App Delegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func grabStoryboard() -> UIStoryboard {
        var storyboard = UIStoryboard()
        let height = UIScreen.mainScreen().bounds.size.height

        if height == 480 {
            storyboard = UIStoryboard(name: "main3.5", bundle: nil)
        } else if height == 568 {
            storyboard = UIStoryboard(name: "main4", bundle: nil)
        } else if height == 667 {
            storyboard = UIStoryboard(name: "main6", bundle: nil)
        } else if height == 736 {
            storyboard = UIStoryboard(name: "main6plus", bundle: nil)
        } else {
            storyboard = UIStoryboard(name: "Main", bundle: nil)
        }

        return storyboard
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.


    UINavigationBar.appearance().barTintColor = color125


        let storyboard: UIStoryboard = self.grabStoryboard()

        self.window?.rootViewController =
            storyboard.instantiateInitialViewController()! as UIViewController
        self.window?.makeKeyAndVisible()
        return true
    }

Customs Class Code

override func awakeFromNib() {
        self.backgroundColor = color125
        self.layer.cornerRadius = 5
        self.setTitleColor(UIColor.blackColor(), forState: .Normal)
}

Upvotes: 0

Views: 237

Answers (2)

Jack G.
Jack G.

Reputation: 3941

The error message indicates that their is a missing link for an outlet which name was "one" ("for the key one").

2016-01-17 16:27:51.151 HHKNBK[36147:32655609] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key one.'

What has happened is an outlet for "one" was initially created (ctrl key drag and drop) but the variable name was changed in the code.

As an example I have created an outlet with the name "one" as well:

@IBOutlet weak var one: UIButton! // Initially working

Then renamed one to changed

@IBOutlet weak var changed: UIButton! // <- Now broken outlet

Now I get the same error message:

2016-01-17 18:56:14.059 InvalidOutlet[10158:5713712] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key one.'

To fix the problem, get rid of the invalid outlet reference by

  1. Opening the storyboard the outlet was defined.
  2. Selecting the component the outlet was defined on.
  3. Opening the "Show the Connections Inspector" (the circle with the arrow on the top right)
  4. Delete the invalid outlet as show in the screen capture

Connections inspector

Upvotes: 1

Ajay Kumar
Ajay Kumar

Reputation: 1827

I think this line is culprit -

@IBOutlet var `as1`: mainbutton!

Remove those quotes

@IBOutlet var as1: mainbutton!

Upvotes: 0

Related Questions