ios - 继续的 UIButton

标签 ios swift xcode uibutton segue

这是我的代码:

import UIKit

class ViewController: UIViewController {

func transfer(controller: UIViewController) {
    let vc = ViewController() //View controller segueing to
    self.present(vc, animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let btn1 = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 30))
    btn1.backgroundColor = UIColor.clear
    btn1.addTarget(self, action: #selector(transfer(controller: ViewController2())), for: UIControlEvents.touchUpInside)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

}

我遇到的问题是: “#selector”的参数不引用“@objc”方法、属性或初始值设定项 我想知道这意味着什么以及如何解决它。我试过用@objc 替换#selector,我试过在#selector 之前添加@objc。感谢您的所有帮助。

最佳答案

Error: argument of #selector does not refer to an @objc method

问题不在于在按钮上添加目标。它带有transfer函数,它应该符合@objc协议(protocol),因为#selector只能引用@objc方法:

保持函数如下:

@objc func transfer() {
    let vc = ViewController() //View controller segueing to
    self.present(vc, animated: true, completion: nil)
}

添加选择器为:

    btn1.addTarget(self, action: #selector(self.transfer), for: UIControlEvents.touchUpInside)

关于ios - 继续的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49736384/

相关文章:

c++ - Qt Quick 2 iOS 缺口回避

ios - 恢复为默认变量值?

ios - 关于自动布局

ios - 为 iOS 10+ 注册通知 - 未收到通知

ios - 如何使用 iPhone 连接到服务器(无浏览器)

ios - 使用 FLAnimatedImage 时获取 EXC_BAD_ACCESS

iphone - MVC 如何正确获取自定义单元格中控件的值?

ios - 尝试设置文本字段委托(delegate)时发生崩溃

ios - 平移手势后将 View 返回到原始位置,swift

swift - 如何快速停止 Sprite 套件更新?