偏离

时间:2018-04-09 15:18:34

标签: 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。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

  

错误: #selector的参数未引用@objc方法

问题不在于在按钮上添加目标。它与函数transfer一致,它应符合@objc协议,因为#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)