Search code examples
swiftxcodeswift4uipickerviewxcode9

Swift make user selection on picker view the title for a button


So, I'm not sure if I've been staring at my code so long I've become dumb or if this is a bit too advanced for my current skills. I am trying to make a pickerview pop up when the user presses a button using DPPickerManager and when the user selects a color the title of the button is supposed to change to what was selected by the user. Flowing the directions I was able to link the button and pickerview but I can't get the button title to change. Here's my code:

    @IBAction func colorPicker(_ sender: UIButton) {
    let condition = ["Blue", "Black", "Green", "Orange", "Purple"]
    DPPickerManager.shared.showPicker(title: "Strings Picker", selected: "Value 1", strings: condition) { (value, ind, cancel) in
        if !cancel {
            //sender.titleLabel.text = value![index].text
            print(value as Any)
        }

    }
}

I keep getting this error: Cannot convert value of type '(Any) -> Int' to expected argument type '(UnboundedRange_) -> ()' and I think its because of the value's index but I am unsure. Can someone please help me?


Solution

  • I have used your code and it's working fine for me.

    @IBAction func btnActionTapped(_ sender: UIButton) {
    
        let condition = ["Blue", "Black", "Green", "Orange", "Purple"]
    
        DPPickerManager.shared.showPicker(title: "String Picker", selected: "Value 1", strings: condition) { (value, index, cancel) in
            if !cancel {
                if let value = value {
                    sender.setTitle(value, for: .normal)
                    print(value)
                }
            }
        }
    }
    

    I just changed following code to get value instead of optional value.

    if let value = value {
        print(value)
    }
    

    I’m getting string value there and print fine in console.