Rumah > Soal Jawab > teks badan
高洛峰2017-04-17 17:50:28
Swift versi yang manakah milik anda? Jika 2.2:
button.addTarget(self, action: #selector(buttonTapped), forControlEvents: .TouchUpInside)
Jika ia adalah versi sebelum 2.2:
button.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)
Saya belum menguji sintaks tertentu, tetapi ia sepatutnya lebih kurang seperti ini.
怪我咯2017-04-17 17:50:28
import UIKit
class BaseViewController: UIViewController {
let button = UIButton(frame: CGRect(x: 100, y: 150, width: 120, height: 50))
override func viewDidLoad() {
super.viewDidLoad()
button.backgroundColor = UIColor.blueColor()
button.setImage(UIImage(named: "2.jpg"), forState: .Highlighted)
button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)
view.addSubview(button)
}
func buttonTapped(sender: UIButton) {
print("hello")
}
}