在Swift 4中添加对UIButton的操作

时间:2017-09-20 04:41:13

标签: ios swift uibutton swift4

当我将Swift 3应用程序迁移到4 Xcode时,我要求在我的几个功能面前添加@objc。 当我这样做时,它会向我显示一个警告:" @objc已被弃用" 我想知道在Swift 4中处理addTarget是否有更好的解决方案?

enter image description here

这是我的UIButton:

let playButton: UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Play", for: .normal)
    btn.setTitleColor(.green, for: .normal)
    btn.addTarget(self, action: #selector(playTapped), for: .touchUpInside)
    return btn
}()

func:

@objc func playTapped() {
        let path = AudioPlayerManager.shared.audioFileInUserDocuments(name: "test")
    AudioPlayerManager.shared.playAudio(path: path)
}

可以在不使用#selector和`@ objc'?

的情况下为按钮添加操作

感谢。

1 个答案:

答案 0 :(得分:-1)

请尝试以下说明

在swift 4中使用swift 3 @objc接口已被弃用

enter image description here