【问题标题】:dismissViewControllerAnimated in swift throws an exceptionswift中的dismissViewControllerAnimated抛出异常
【发布时间】:2015-02-25 07:38:31
【问题描述】:

我是快速编程的新手。我正在尝试在 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.
    }
}

当我试图关闭视图控制器时遇到此异常

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

【问题讨论】:

  • 您可能忘记将视图控制器故事板 ID 命名为“SecVC”。仔细检查一下
  • Interface Builder 文件中的未知类 VC2。无法识别的选择器发送到实例
  • @LeonardoSavioDabus 我检查了它。故事板 ID 是“SecVC”
  • 去你的检查员那里检查自定义类是否在下拉菜单中选择了正确的控制器(我认为是 VC2 控制器)
  • 是的,我选择了正确的控制器@LeonardoSavioDabus

标签: ios swift exception dismiss


【解决方案1】:

该错误表明您尚未设置视图控制器类 VC2 或 VC1 或者您更改了后退函数的名称或类似名称但忘记在情节提要中更新。

【讨论】:

  • thanq Arbitur 我找到了解决方案
  • 很高兴你发现了问题:)
【解决方案2】:

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

【讨论】:

  • 感谢您的快速回复。我得到了解决方案并发布了它来解决我的问题。
【解决方案3】:

我找到了一些回答它的解决方案对我有用.....

Xcode 有时会错过 customModule="AppName" customModuleProvider="target"

要修复它,请将情节提要作为源代码打开并替换此行:

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

到这里:

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

【讨论】:

  • 我重命名了一个 IBAction,但没有从情节提要文件中删除。我手动编辑以删除该 IBAction,然后它起作用了。希望这对某人有所帮助。
【解决方案4】:

如果您使用情节提要,请务必检查情节提要中 View Controller 上的模块,位于 Identity Inspector(您设置自定义类名的位置)下 - 特别是如果您像我一样更改了应用名称。由于某种原因,我的没有设置为继承。

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 2015-11-22
    • 2013-05-24
    • 2016-01-03
    • 2018-01-08
    • 2012-01-24
    • 2011-05-30
    • 1970-01-01
    相关资源
    最近更新 更多