Я пытаюсь программно настроить кнопку. Я вызываю функцию следующим образом:
Код: Выделить всё
override func viewDidLoad() {
super.viewDidLoad()
var yCord1 = 100
var yCord2 = 200
setUpButton("YES", yCord1)
setUpButton("NO", yCord2)
}
Код: Выделить всё
func setupButton(_ text:String, _ yCord:Int) {
let myButton = UIButton()
myButton.setTitle(text, for: .normal)
myButton.setTitleColor(.white, for: .normal)
myButton.backgroundColor = .blue
myButton.frame = CGRect(x: 100, y: yCord, width: 100, height: 50)
// this didn't work:
// myButton.addAction(selector(tapButton()), for: .touchUpInside)
// this also didn't work..\
myButton.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
// ADD TO VIEW
self.view.addSubview(myButton)
}
Код: Выделить всё
func tapButton(_ text:String) {
if text == "YES" {
print("Yes button has been tapped!")
} else {
print("NO button tapped")
}
myButton.addAction(...
или
myButton.addTarget(...
Мне не удалось заставить ни один из них работать, я застрял в том, какой код добавить в часть действия - for: touchUpInside, кажется, работает нормально. Я также не нашел пример, в котором я могу передать значение. Если это невозможно, я думаю, что смогу найти обходной путь. Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/798 ... n-in-swift
Мобильная версия