Search code examples
iosswiftdropdownbox

how would I know what button to place the selected content of the dropdown box in. Swift


I am using this dropdown box in my project: https://github.com/kirkbyo/Dropper

I want to enter the selected item from the drop-down box into the text of the button. However, I have 3 buttons which all with drop down boxes and to find out what item has been pressed you use the func DropperSelectedRow(_ path: IndexPath, contents: String) method which will give you the index path and string of what was clicked. in which case you just place the content into the button text but I don't know which button to place it in. How would I find this out?

this is my code:

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)
    }

How would I place the contents of what was clicked into the button text of where the dropper came from? Thank you.


Solution

  • Use func DropperSelectedRow(_ path: IndexPath, contents: String, tag: Int) instead. You can assign a tag for your buttons and check this property when delegate method is invoked.

    For example: typeDropper.tag = 1

    And

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