0

I am new to swift programming. I am trying to present VC1.Swift on VC2.Swift, and dismiss VC2.swift to VC1.swift. At the time presenting it working but I am facing an exception at the time of dismissViewController. This is my code

VC1.Swift

import UIKit
import Foundation


class VC1: UIViewController
{

    @IBOutlet var buttonDeclare: UIButton!

    @IBAction func btnPressed()
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("SecVC") as UIViewController
        self.presentViewController(vc, animated: true, completion: nil)
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }


}

VC2.Swift

import UIKit

class VC2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    @IBAction func backBtn(sender: AnyObject)
    {
         self.dismissViewControllerAnimated(true, completion: nil)
    }

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

This exception I am getting when I am trying to dismiss the viewcontroller

015-02-25 12:50:55.890 My first Swiftapp[3418:53740] Unknown class VC2 in Interface Builder file.
2015-02-25 12:50:56.898 My first Swiftapp[3418:53740] -[UIViewController backBtn:]: unrecognized selector sent to instance 0x7fea2ad26ba0
2015-02-25 12:50:56.950 My first Swiftapp[3418:53740] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController backBtn:]: unrecognized selector sent to instance 0x7fea2ad26ba0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f2e5f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000011106cbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f2ed04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010f24527c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010f244e18 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010fdb58be -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x000000010febc410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x000000010febb7df -[UIControl touchesEnded:withEvent:] + 522
    8   UIKit                               0x000000010fdfb308 -[UIWindow _sendTouchesForEvent:] + 735
    9   UIKit                               0x000000010fdfbc33 -[UIWindow sendEvent:] + 683
    10  UIKit                               0x000000010fdc89b1 -[UIApplication sendEvent:] + 246
    11  UIKit                               0x000000010fdd5a7d _UIApplicationHandleEventFromQueueEvent + 17370
    12  UIKit                               0x000000010fdb1103 _UIApplicationHandleEventQueue + 1961
    13  CoreFoundation                      0x000000010f21b551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010f21141d __CFRunLoopDoSources0 + 269
    15  CoreFoundation                      0x000000010f210a54 __CFRunLoopRun + 868
    16  CoreFoundation                      0x000000010f210486 CFRunLoopRunSpecific + 470
    17  GraphicsServices                    0x00000001143219f0 GSEventRunModal + 161
    18  UIKit                               0x000000010fdb4420 UIApplicationMain + 1282
    19  My first Swiftapp                   0x000000010f10342e top_level_code + 78
    20  My first Swiftapp                   0x000000010f10346a main + 42
    21  libdyld.dylib                       0x0000000111846145 start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Paulw11
  • 108,386
  • 14
  • 159
  • 186
S P Balu Kommuri
  • 890
  • 10
  • 28

4 Answers4

1

That error indicates you havent set the viewcontrollers class VC2 or VC1 or that you changed the name of the back function or something similar but forgot to update in storyboard.

Arbitur
  • 38,684
  • 22
  • 91
  • 128
1

Just make sure your back button is connected to your ibaction or segue!!!

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
1

i find the solution some on answered it works for me.....

Sometimes Xcode missed customModule="AppName" customModuleProvider="target"

To fix it, open storyboard as source code and replace this line:

<viewController storyboardIdentifier="StoryboardId" id="SomeID" customClass="CustomClass"
sceneMemberID="viewController">

to this:

<viewController storyboardIdentifier="StoryboardId" id="SomeID" customClass="CustomClass"
 customModule="AppName" customModuleProvider="target" sceneMemberID="viewController">
S P Balu Kommuri
  • 890
  • 10
  • 28
  • I have renamed an IBAction, but wasn't removed from the storyboard file. I manually edited to remove that IBAction and then it worked. Hope this helps someone. – Monster Brain Jul 23 '18 at 11:07
0

If you're using storyboards, make sure to check the module on the View Controller in Storyboard under Identity Inspector (Where you set custom class names) - especially if you changed your app name like I did. For some reason, mine wasn't set to inherit.

Identity Inspector

froggomad
  • 1,747
  • 2
  • 17
  • 40