【问题标题】:only instance methods can be declared @IBAction building audio app只能声明实例方法 @IBAction 构建音频应用程序
【发布时间】:2021-09-20 18:41:50
【问题描述】:

我一直在尝试构建一个允许我单击按钮并播放音频的 IOS 应用程序,但我遇到了这个错误,只有实例方法可以声明为 @IBAction。我进行了搜索,似乎我没有将它从课堂上关闭或类似的东西,但它仍然不想工作

import UIKit
import AVFoundation
            
class LevelAStartingSoundViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


@IBAction func Sbutton(_ sender: Any, forEvent event: UIEvent) {
        playSound(sound: "Ssound", type: "wav")
    
    
    }
}
}

【问题讨论】:

    标签: swift uikit swift5 ibaction


    【解决方案1】:

    如果您重新格式化您的代码 (Ctrl + i),您会注意到Sbutton 在内部 viewDidLoad

    你需要把它移到外面。

    @IBAction func sButton(_ sender: UIButton) { /// make sure you have the right function signature
        playSound(sound: "Ssound", type: "wav")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    

    【讨论】:

    • 我已经重新配置了我的代码,现在我收到了这个错误“无法识别的选择器已发送到实例”,供参考的屏幕截图link
    • @NickSchofield 看到我的编辑。还转到情节提要,单击您的按钮,然后使用助理编辑器重新连接IBAction。 (如果存在旧的,您可能还需要删除它,您可以在右侧面板中最后一个图标的 Connections Inspector 中执行此操作)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多