swift中的dismissViewControllerAnimated抛出异常

时间:2015-02-25 07:38:32

标签: ios swift exception dismiss

我是快速编程的新手。我试图在VC2.Swift上呈现VC1.Swift,并将VC2.swift解除为VC1.swift。在提交它的时候,我在dismissViewController时面临例外。 这是我的代码

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.
    }
}

当我尝试关闭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

4 个答案:

答案 0 :(得分:1)

该错误表示您尚未设置viewcontrollers类VC2或VC1 或者您更改了后退功能的名称或类似但忘记在故事板中更新。

答案 1 :(得分:1)

只需确保您的后退按钮已连接到您的ibaction或segue !!!

答案 2 :(得分:0)

我发现解决方案有些回答它对我有用.....

有时Xcode错过了customModule =“AppName”customModuleProvider =“target”

要解决此问题,请打开storyboard作为源代码并替换此行:

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

到此:

<viewController storyboardIdentifier="StoryboardId" id="SomeID" customClass="CustomClass"
 customModule="AppName" customModuleProvider="target" sceneMemberID="viewController">

答案 3 :(得分:0)

如果您使用的是故事板,请确保检查“故事板”中View Controller上“身份检查器”下的模块(在其中设置自定义类名称)-尤其是像我一样更改应用程序名称时。由于某种原因,我的继承权未设置。

Identity Inspector