【问题标题】:Why do I get an "unrecognized selector" error with my IBAction?为什么我的 IBAction 出现“无法识别的选择器”错误?
【发布时间】:2020-01-07 23:58:31
【问题描述】:

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

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:]:无法识别的选择器发送到实例 0x7ff27fe2d480 2019-09-05 15:03:06.102115+0900 testap[24311:376425] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[testap.ViewController Detailfuc:]:无法识别的选择器发送到实例 0x7ff27fe2d480” *** 首先抛出调用堆栈: ( 0 核心基础 0x0000000105a618db __exceptionPreprocess + 331 1 libobjc.A.dylib 0x0000000103fb8ac5 objc_exception_throw + 48 2核心基础0x0000000105a7fc94-[NSObject(NSObject)不识别选择器:]+132 3 UIKitCore 0x000000010f0bb235-[UIResponder doesNotRecognizeSelector:] + 287 4 核心基础 0x0000000105a66623 ___转发___ + 1443 5 核心基础 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 发送事件:] + 4079 12 UIKitCore 0x000000010f0a930e -[UIApplication sendEvent:] + 356 13 UIKitCore 0x000000010f1792b3 __dispatchPreprocessedEventFromEventQueue + 3232 14 UIKitCore 0x000000010f17bbd9 __handleEventQueueInternal + 5911 15 核心基础 0x00000001059c8db1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 16 核心基础 0x00000001059c8633 __CFRunLoopDoSources0 + 243 17 核心基础 0x00000001059c2cef __CFRunLoopRun + 1231 18 核心基础 0x00000001059c24d2 CFRunLoopRunSpecific + 626 19 图形服务 0x000000010a69f2fe GSEventRunModal + 65 20 UIKitCore 0x000000010f08efc2 UIApplicationMain + 140 21 测试 0x00000001035c31eb 主要 + 75 22 libdyld.dylib 0x0000000107dcf541 开始 + 1 ) l ibc++abi.dylib:以 NSException 类型的未捕获异常终止

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

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    如您所见:

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

    这不是问题。崩溃的原因是您将Detailfuc 重命名为DetailFuc,大写为F。所以找不到原来的小写f函数,崩溃了。

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

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

    【讨论】:

      【解决方案2】:

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

      当我在 Xcode 8 beta 6 中尝试相同时:

      我拖到类别文件并说使用发送方 UIButton 开发一个动作,并将其命名为“doPrint”。

      Xcode 生成了这个:

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

      当您希望使用 Objective C 选择器“doPrint:”时,您必须在第一个参数之前放置'_',在 Swift 中是第三个。 更多信息here

      【讨论】:

      • Xcode 8?那是好几年了。你是说 Xcode 11 吗?
      【解决方案3】:

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

      当您将情节提要中的插座对象连接到视图控制器中的代码时,您无法在断开连接之前随时修改名称,方法是右键单击情节提要中的对象并按下链接对象与代码,并在从代码中修改名称后再次链接它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-04
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 2019-10-26
        • 1970-01-01
        相关资源
        最近更新 更多