这是我的代码:
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。谢谢你的帮助。
答案 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)