为什么我的IBAction出现“无法识别的选择器”错误?

时间:2019-09-05 06:17:51

标签: ios swift

我想通过链接打开网页。我正在使用该地址作为全局变量。但是,当我运行全局函数时,会发生错误。是什么原因?

GlobalValue.swift

import Foundation

struct  Global {
    let apiAddress = "http://11.111.111.111:11111/"
    let LinkURL: String
    let LinkSecond : String

    init()
    {
        agreeLinkURL = "User?type=webview"
        agreeLinkSecond = "User2?type=webview"
    }

    func getURL() -> String {
        return apiAddress + LinkURL
    }

    func getURLSecond() -> String {
        return apiAddress + LinkSecond
    }
}

用法

let dataUrl = Global()

class ViewController: UIViewController {
...
    @IBAction func DetailFuc(_ sender: Any) {
        let detailUrl = dataUrl.getURL() // get error
        print("***********************************************")
        print(detailUrl)
        print("***********************************************")
        if let appURL = URL(string: detailUrl) {
            UIApplication.shared.open(appURL) { success in
                if success {
                    print("The URL was delivered successfully.")
                } else {
                    print("The URL failed to open.")
                }
            }
        } else {
            print("Invalid URL specified.")
        }
    }

错误是:

2019-09-05 15:03:06.094723+0900 testap[24311:376425] -[testap.ViewController Detailfuc:]: unrecognized selector sent to instance 0x7ff27fe2d480
2019-09-05 15:03:06.102115+0900 testap[24311:376425] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[testap.ViewController Detailfuc:]: unrecognized selector sent to instance 0x7ff27fe2d480'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105a618db __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x0000000103fb8ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000105a7fc94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKitCore                           0x000000010f0bb235 -[UIResponder doesNotRecognizeSelector:] + 287
    4   CoreFoundation                      0x0000000105a66623 ___forwarding___ + 1443
    5   CoreFoundation                      0x0000000105a68418 _CF_forwarding_prep_0 + 120
    6   UIKitCore                           0x000000010f090624 -[UIApplication sendAction:to:from:forEvent:] + 83
    7   UIKitCore                           0x000000010eae58d5 -[UIControl sendAction:to:forEvent:] + 67
    8   UIKitCore                           0x000000010eae5bf2 -[UIControl _sendActionsForEvents:withEvent:] + 450
    9   UIKitCore                           0x000000010eae4ba8 -[UIControl touchesEnded:withEvent:] + 583
    10  UIKitCore                           0x000000010f0c94e6 -[UIWindow _sendTouchesForEvent:] + 2547
    11  UIKitCore                           0x000000010f0cabca -[UIWindow sendEvent:] + 4079
    12  UIKitCore                           0x000000010f0a930e -[UIApplication sendEvent:] + 356
    13  UIKitCore                           0x000000010f1792b3 __dispatchPreprocessedEventFromEventQueue + 3232
    14  UIKitCore                           0x000000010f17bbd9 __handleEventQueueInternal + 5911
    15  CoreFoundation                      0x00000001059c8db1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x00000001059c8633 __CFRunLoopDoSources0 + 243
    17  CoreFoundation                      0x00000001059c2cef __CFRunLoopRun + 1231
    18  CoreFoundation                      0x00000001059c24d2 CFRunLoopRunSpecific + 626
    19  GraphicsServices                    0x000000010a69f2fe GSEventRunModal + 65
    20  UIKitCore                           0x000000010f08efc2 UIApplicationMain + 140
    21  testap                             0x00000001035c31eb main + 75
    22  libdyld.dylib                       0x0000000107dcf541 start + 1
)
l

ibc++abi.dylib: terminating with uncaught exception of type NSException

设置全局变量时,一定有问题,但是我不知道出了什么问题。请让我知道如何解决此问题。调用网页的方法正确吗?

3 个答案:

答案 0 :(得分:0)

如您所见:

  

原因:“-[testap.ViewController Detailfuc:]:无法识别的选择器已发送到实例0x7ff27fe2d480”

这不是问题。崩溃的原因是您将大写Detailfuc的{​​{1}}重命名为DetailFuc。因此它找不到具有小写字母F并崩溃的原始函数。

每次更改目标时,都应从按钮上断开错误的IBAction。

您可以右键单击按钮以查看连接,然后单击x以使用小写f的错误连接

答案 1 :(得分:0)

您是否一直在使用旧版本的Xcode 8 Beta?

当我在Xcode 8 Beta 6中尝试相同的操作时:

我将鼠标移到类别文件上,说使用发件人UIButton以及标题“ doPrint”来开发一个动作。

Xcode生成了这个:

@IBAction func doPrint(_ sender: AnyObject) {  
}  

当您希望使用Objective C选择器“ doPrint:”创建方法时,必须在Swift 3的第一个参数之前放置“ _”。 更多信息here

答案 2 :(得分:0)

(原因:'-[testap.ViewController Detailfuc :]:无法识别的选择器)

将情节提要中的插座对象连接到视图控制器中的代码时,在右键单击情节提要中的对象并按链接该主题的x按钮时,无法在断开名称之前随时修改名称。对象与代码,然后在代码中修改名称后,再次将其链接。