'unrecognized selector sent to instance 0x7fd167f06120' terminating with uncaught exception of type NSException'
I have been looking for the answer for hours now, and even on Stack Overflow, no one seems to have the answer. Any help is appreciated!
Here is the code for my DropDownMenu class...
Code Block class DropDownMenu: UIView { var main: UIButton! = UIButton(frame: CGRect(x: 0, y: 0, width: 46, height: 30)) var view: UIView! var buttonTitles: [String]! = [""] var titleColor: UIColor! = UIColor.black var font: UIFont! = UIFont.systemFont(ofSize: 18) var buttonsBorderWidth: CGFloat! = 0 var buttonsBorderColor: UIColor? = UIColor.white var buttonsCornerRadius: CGFloat! = 0 var color: UIColor! = UIColor.clear var buttonsImageEdgeInsets: UIEdgeInsets? = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) var images: [UIImage]? = nil var target: UIViewController! private var currentSelected: String? = nil private var optionsStack = UIStackView() init(main: UIButton) { self.main = main super.init(frame: CGRect()) } func createDropDownMenu() { main.addTarget(target, action: #selector(DropDownMenu.openDropdown(_:)), for: .touchUpInside) print("Button Target?: \(main.allTargets), self.target: \(String(describing: target))") let mainFrame = main.frame optionsStack.frame = CGRect(x: mainFrame.minX, y: mainFrame.maxY, width: mainFrame.width, height: CGFloat(buttonTitles.count) * mainFrame.height) optionsStack.axis = .vertical view.addSubview(optionsStack) var y: CGFloat! = 0 for title in buttonTitles { let button = UIButton(frame: CGRect(x: 0, y: y, width: mainFrame.width, height: mainFrame.height)) button.setTitle(title, for: .normal) button.setTitleColor(titleColor, for: .normal) button.backgroundColor = color button.titleLabel?.font = font button.addTarget(target, action: #selector(DropDownMenu.onclick), for: .touchUpInside) y += mainFrame.height optionsStack.addArrangedSubview(button) } for button in optionsStack.arrangedSubviews { button.isHidden = true button.alpha = 0 } } @objc private func openDropdown(_ sender: UIButton) { print("sender: \(String(describing: sender))") optionsStack.arrangedSubviews.forEach { (button) in UIView.animate(withDuration: 0.7) { button.isHidden = !button.isHidden button.alpha = button.alpha == 0 ? 1 : 0 self.view.layoutIfNeeded() } } } @objc private func onclick(_ sender: UIButton) { let title = sender.titleLabel!.text print(title as Any) main.setTitle(title, for: .normal) optionsStack.arrangedSubviews.forEach { (button) in UIView.animate(withDuration: 0.7) { button.isHidden = true button.alpha = 0 } } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
Here is the code for the creation of the object in ViewContorller...
let main = UIButton(frame: CGRect(x: 50, y: 300, width: 80, height: 30))
main.layer.borderWidth = 1
main.setTitle("Grade", for: .normal)
main.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
main.setTitleColor(UIColor.black, for: .normal)
let gradeDP = DropDownMenu(main: main)
gradeDP.buttonTitles = ["Title 1", "Title 2", "Title 3"]
gradeDP.color = UIColor.gray
gradeDP.target = self
gradeDP.titleColor = UIColor.white
gradeDP.view = infoBox
This is all of my code...