ios - 我没有在 pod 'DropDown' 中得到我选择的值

标签 ios swift xcode cocoapods dropdown

我已经在我的项目中安装了 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

}

最佳答案

您需要创建 UIButtonoutlet 或从方法参数 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/

相关文章:

xcode - 如何使用 XCode 5 构建 IPA 以使用 TestFlight 进行分发?

ios - XCode 为 iPad 构建应用程序但不允许调试

ios - 我如何使用苹果的标准 Controller 来重现这种行为?

iphone - 文本到语音 [TTS] TTSPlaybackCreate 无法初始化动态 : -3000

swift - 创建 Swift 框架(重新访问)

ios - 为什么我的 Swift 应用程序没有 AppDelegate 或 SceneDelegate?

ios - Assets 目录启动图像未用于 4 英寸模拟器

javascript - 如何使用 JavaScriptCore 在 Swift 中从 JavaScript SDK 调用异步 JavaScript 函数

ios - 为什么可选 ("Text") - Swift

c++ - Swift - 使用 Objective-C 包装器调用 C++ 文件 - 传递 Int 值