我已经在我的项目中安装了 pod 'DropDown'
。当我按下按钮时,它会显示带有月份列表的下拉菜单,当我从列表中选择月份时,它应该显示在按钮的位置,就像每个下拉菜单一样有效,但我无法获得我选择的值。按钮空间保持空白。
var dropDown = DropDown()
@IBAction func dropDownButtonTapped(_ sender: UIButton) {
dropDown.anchorView = dropDownButton
dropDown.dataSource = ["1","2","3","4","1","2","3","4"]
dropDown.show()
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
}
dropDown.direction = .bottom
}
最佳答案
您需要创建 UIButton
的 outlet
或从方法参数 sender
获取按钮。
下面我创建了 socket ,并在 dropDown.selectionAction
中设置了选择时的按钮标题。
class ViewController: UIViewController {
let dropDown = DropDown()
@IBOutlet weak var menuButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
dropDown.anchorView = menuButton
dropDown.dataSource = ["1","2","3","4","1","2","3","4"]
}
@IBAction func dropDownButtonTapped(_ sender: UIButton) {
dropDown.show()
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
self.menuButton.setTitle(item,for: .normal)
}
}
}
干杯!
关于ios - 我没有在 pod 'DropDown' 中得到我选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353391/