Search code examples
iosswiftautolayoutuialertcontrolleruialertaction

Getting error on clicking button in swift


I am a newbie to iOS development and just started learning basic stuffs in swift. Now I am getting error while i try to run my basic coding that I am practising with Auto Layout. The below exceptions occurs while I try to open a dialog on clicking the button. I am also posting my sample code for your reference

The exception I am getting is as follows:

<NSLayoutConstraint:0x79e84ca0 V:[UILabel:0x79e82b40'sadadsfsadfsd']-(90)-[_UILayoutGuide:0x79e84610]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-01 00:04:18.833 FirstDemo[6922:355986] -[FirstDemo.ViewController showSomething]: unrecognized selector sent to instance 0x79e80210
2015-10-01 00:04:18.836 FirstDemo[6922:355986] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstDemo.ViewController showSomething]: unrecognized selector sent to instance 0x79e80210'
*** First throw call stack:
(
    0   CoreFoundation                      0x0025aa94 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01e5ce02 objc_exception_throw + 50
    2   CoreFoundation                      0x00263de3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x001a1e3d ___forwarding___ + 1037
    4   CoreFoundation                      0x001a1a0e _CF_forwarding_prep_0 + 14
    5   libobjc.A.dylib                     0x01e710b5 -[NSObject performSelector:withObject:withObject:] + 84
    6   UIKit                               0x00a3dc40 -[UIApplication sendAction:to:from:forEvent:] + 118
    7   UIKit                               0x00a3dbbf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    8   UIKit                               0x00bd38fc -[UIControl sendAction:to:forEvent:] + 79
    9   UIKit                               0x00bd3c7c -[UIControl _sendActionsForEvents:withEvent:] + 408
    10  UIKit                               0x00bd2c87 -[UIControl touchesEnded:withEvent:] + 714
    11  UIKit                               0x00ab4eb8 -[UIWindow _sendTouchesForEvent:] + 1095
    12  UIKit                               0x00ab5da4 -[UIWindow sendEvent:] + 1118
    13  UIKit                               0x00a5e9b3 -[UIApplication sendEvent:] + 266
    14  UIKit                               0x00a35903 _UIApplicationHandleEventQueue + 7327
    15  CoreFoundation                      0x00174e7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    16  CoreFoundation                      0x0016ab0b __CFRunLoopDoSources0 + 523
    17  CoreFoundation                      0x00169f28 __CFRunLoopRun + 1032
    18  CoreFoundation                      0x00169866 CFRunLoopRunSpecific + 470
    19  CoreFoundation                      0x0016967b CFRunLoopRunInMode + 123
    20  GraphicsServices                    0x045fa664 GSEventRunModal + 192
    21  GraphicsServices                    0x045fa4a1 GSEventRun + 104
    22  UIKit                               0x00a3bcc1 UIApplicationMain + 160
    23  FirstDemo                           0x000784fc main + 140
    24  libdyld.dylib                       0x028cda21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }


    @IBAction func showMessage(){

        let alertController = UIAlertController(title:"Success",message:"Awesome done",preferredStyle:UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

Please help me. Kindly text me if my question is not clear. Thanks in advance


Solution

  • So in interface builder you likely created an IBAction attached to your button. It looks like it was originally named 'showSomething' and you renamed it to 'showMessage'.

    To fix the bug you either can change the name of your method back to 'showSomething', or you'll have to go into interface builder, select your button, and check the outlet connections (the button on the upper right that's a circle with an arrow in it).

    You'll see your old outlet connection to showSomething, delete it and reconnect your button to your new function 'showMessage'.