我怎么知道将下拉框的选定内容放入哪个按钮。Swift

时间:2018-11-21 11:14:38

标签: ios swift dropdownbox

我在我的项目中使用此下拉框:https://github.com/kirkbyo/Dropper

我想将下拉框中的所选项目输入按钮的文本中。但是,我有3个按钮,所有按钮均带有下拉框,并使用func DropperSelectedRow(_ path: IndexPath, contents: String)方法来查找已按下的项目,该方法将为您提供所单击内容的索引路径和字符串。在这种情况下,您只是将内容放置在按钮文本中,但我不知道将其放置在哪个按钮中。我如何找到它?

这是我的代码:

class ListComposerTableViewController: UITableViewController, UITextViewDelegate, DropperDelegate {

    @IBOutlet weak var typeButton: UIButton!
    @IBOutlet weak var dateButton: UIButton!
    @IBOutlet weak var publicButton: UIButton!

    let typeDropper = Dropper(width: 105, height: 105)
    let typeOptions = ["Event", "Birthday", "Christmas", "Charity"]

    let publicDropper = Dropper(width: 105, height: 105)
    let publicOptions = ["Public", "Private"]

    let dueDropper = Dropper(width: 105, height: 130)
    let dueOptions = ["Bithday", "Christmas", "Custom Date"]

    override func viewDidLoad() {
        super.viewDidLoad()

        //set up the droppers

        typeDropper.items = typeOptions // Item displayed
        typeDropper.maxHeight = 105
        typeDropper.theme = Dropper.Themes.black(UIColor.white)
        typeDropper.delegate = self
        typeDropper.cornerRadius = typeButton.layer.cornerRadius
        typeDropper.cellColor = Colours.flatColour.main.headings
        typeDropper.cellBackgroundColor = UIColor.white
        typeDropper.width = 105
        typeDropper.height = 105

        publicDropper.items = publicOptions // Item displayed
        publicDropper.maxHeight = 105
        publicDropper.theme = Dropper.Themes.black(UIColor.white)
        publicDropper.delegate = self
        publicDropper.cornerRadius = publicButton.layer.cornerRadius
        publicDropper.cellColor = Colours.flatColour.main.headings
        publicDropper.cellBackgroundColor = UIColor.white
        publicDropper.width = 105
        publicDropper.height = 105  

        dueDropper.items = dueOptions // Item displayed
        dueDropper.maxHeight = 130
        dueDropper.theme = Dropper.Themes.black(UIColor.white)
        dueDropper.delegate = self
        dueDropper.cornerRadius = publicButton.layer.cornerRadius
        dueDropper.cellColor = Colours.flatColour.main.headings
        dueDropper.cellBackgroundColor = UIColor.white
        dueDropper.width = 105
        dueDropper.height = 105
    }

    @IBAction func typeDidPress(_ sender: Any) {
        if typeDropper.status == .hidden {
            typeDropper.showWithAnimation(0.15, options: Dropper.Alignment.center, button: typeButton)
        } else {
            typeDropper.hideWithAnimation(0.1)
        }
    }
    @IBAction func dueDidPress(_ sender: Any) {
        if dueDropper.status == .hidden {
            dueDropper.showWithAnimation(0.15, options: Dropper.Alignment.center, button: dateButton)
        } else {
            dueDropper.hideWithAnimation(0.1)
        }
    }

    @IBAction func publicDidPress(_ sender: Any) {
        if publicDropper.status == .hidden {
            publicDropper.showWithAnimation(0.15, options: Dropper.Alignment.center, button: publicButton)
        } else {
        publicDropper.hideWithAnimation(0.1)
        }
    }

    func DropperSelectedRow(_ path: IndexPath, contents: String) {
        print(path)
        print(contents)
    }

我如何将单击的内容放入滴管来自的按钮文本中?谢谢。

1 个答案:

答案 0 :(得分:1)

改为使用func DropperSelectedRow(_ path: IndexPath, contents: String, tag: Int)。 您可以为按钮分配标签,并在调用委托方法时检查此属性。

例如:typeDropper.tag = 1

func DropperSelectedRow(_ path: IndexPath, contents: String, tag: Int) {
    if tag == 1 {
        typeButton.setTitle(contents, for: .normal)
    }
}