Свойство button.addTarget не переносится в функцию
Я сделал пользовательский класс объектов всплывающих уведомлений в Swift. Одной из функций этого объекта является добавление кнопок в себя, представление уведомлений (см. AddButtons).
import UIKit
class PopUpNotification: NSObject {
let blackView = UIView()
var notificationButtons = [UIButton]()
override init() {
super.init()
}
func show(){
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
window.addSubview(blackView)
blackView.frame = window.frame
blackView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
self.blackView.alpha = 1
})
blackView.addSubview(notificationView)
setupNotification()
}
}
func dismiss(){
UIView.animate(withDuration: 0.5, animations: {
self.blackView.alpha = 0
})
}
func setNotificationText(_ text: String){
notificationText.text = text
}
func addButtons(buttons: UIButton...){
for button in buttons {
button.backgroundColor = UIColor.RED
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont(name: "Avenir-Black", size: 10)
button.layer.cornerRadius = 20
button.translatesAutoresizingMaskIntoConstraints = false
notificationButtons.append(button)
}
}
lazy var notificationView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.white
view.layer.cornerRadius = 15
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let notificationText: UITextView = {
let label = UITextView()
label.textColor = UIColor.black
label.textAlignment = .center
label.backgroundColor = UIColor.clear
label.isEditable = false
label.font = UIFont(name: "Avenir-Black", size: 19)
label.sizeToFit()
label.isScrollEnabled = false
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setupNotification(){
notificationView.centerXAnchor.constraint(equalTo: blackView.centerXAnchor).isActive = true
notificationView.centerYAnchor.constraint(equalTo: blackView.centerYAnchor).isActive = true
notificationView.leftAnchor.constraint(equalTo: blackView.leftAnchor, constant: 30).isActive = true
notificationView.rightAnchor.constraint(equalTo: blackView.rightAnchor, constant: -30).isActive = true
notificationView.heightAnchor.constraint(equalToConstant: CGFloat(120 + (50 * notificationButtons.count)) + notificationText.frame.size.height).isActive = true
var bottomAnchor: NSLayoutYAxisAnchor?
bottomAnchor = notificationView.bottomAnchor
for button in notificationButtons {
notificationView.addSubview(button)
button.bottomAnchor.constraint(equalTo: bottomAnchor!, constant: -10).isActive = true
button.rightAnchor.constraint(equalTo: notificationView.rightAnchor, constant: -20).isActive = true
button.leftAnchor.constraint(equalTo: notificationView.leftAnchor, constant: 20).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
bottomAnchor = button.topAnchor
}
notificationView.addSubview(notificationText)
notificationText.bottomAnchor.constraint(equalTo: bottomAnchor!, constant: -10).isActive = true
notificationText.topAnchor.constraint(equalTo: notificationView.topAnchor, constant: 5).isActive = true
notificationText.leftAnchor.constraint(equalTo: notificationView.leftAnchor, constant: 5).isActive = true
notificationText.rightAnchor.constraint(equalTo: notificationView.rightAnchor, constant: -5).isActive = true
}
}
Я собираюсь использовать этот класс, чтобы сначала создать кнопку, добавить цель к этой кнопке, затем инициализировать объект PopUpNotification и, наконец, добавить созданную кнопку. В конце мне нужна кнопка, чтобы перенести свою цель на уведомление. Вот как я использую это:
let notification: PopUpNotification = {
let okButton = UIButton()
okButton.setTitle("OK", for: .normal)
okButton.addTarget(self, action: #selector(printHello), for: .touchUpInside)
let popUp = PopUpNotification()
popUp.setNotificationText("Some notification text in here bla bla bla")
popUp.addButtons(buttons: okButton )
return popUp
}()
func printHello(){
print("Hello there")
}
В XCode 8 все работало нормально. Появилось всплывающее уведомление, и после нажатия кнопки "ОК" было напечатано "Hello". Проблема, с которой я сталкиваюсь сейчас, заключается в том, что после обновления XCode до 9 цель кнопки, похоже, не переносится. Кнопка замечательно отображается в уведомлении, но не работает, не печатает "Hello".
Кто-нибудь знает почему? Это ошибка XCode/swift или я что-то здесь упускаю?