Set Title of UIButton from Selected Row of UIPickerView in swift

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 07:01:55

问题


I am trying to set my Button title from a selected row from uipickerview. However, I can't seem to find a valid solution.

I have set the variables to be global thus I can access the Selected Variable. However I cant seem to be able to pinpoint exactly when the user clicks the exit button and able to update my button title.

Things I have tried:

  • Global Functions ( couldn't work because you cant specify the button you want to change the name of )
  • Dispatchqueue.main.async - Did not update my title. The idea here is for it to constantly check for a string change.

The solution I found here is in Obj-C. I tried converting it to swift https://objectivec2swift.com/#/converter/code/ with no luck. Set Title of UIButton from Selected Row of UIPickerView

@IBAction func CountryPickButton(_ sender: UIButton) {

        Global.setPickerDataSource(data:["Test","test2","test3"])
        Global.setPickerCompletionHandler(int:0)
        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PickerViewPopUpId") as! PickerViewController
        //CountryPickButtonDetector = sender as? UIButton
        self.addChild(popOverVC)
        popOverVC.view.frame = self.view.frame
        self.view.addSubview(popOverVC.view)
        popOverVC.didMove(toParent: self)
        popOverVC.callback { value in
        //CountryPickButton.title.text = value
        sender.setTitle(value, for: UIControl.State)
    }
    }

import UIKit
var test = ""

class PickerViewController: UIViewController {
    @IBOutlet weak var PickerView: UIPickerView!
    var callback : ((String) -> Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
        PickerView.dataSource = self
        PickerView.delegate = self
        // Do any additional setup after loading the view.
    }
    @IBAction func ExitButton(_ sender: Any) {
        switch Global.pickerCompletionHandler {

        case 0:
            callback?(Global.pickerResult ?? "")
            Global.setProfileCountry(string:
                Global.pickerResult ?? "")
        default:
            print("nothing")

        }
        self.view.removeFromSuperview()
    }

}

extension PickerViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1 // can be more than 1 component like time/date/year
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return Global.pickerDataSource?.count ?? 1
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //Global.pickerResult = Global.pickerDataSource?[row]
        Global.setPickerResult(result: Global.pickerDataSource?[row] ?? "Test" )
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return Global.pickerDataSource?[row]
    }


}

Globals

import Foundation

struct Global {
    static var pickerDataSource:[String]? = nil
    static var pickerResult:String? = nil
    static var pickerCompletionHandler:Int? = 0
    static var profileCountry:String? = nil

    static func setPickerDataSource(data:[String]) {
        Global.pickerDataSource = data
    }
    static func setPickerResult(result:String) {
        Global.pickerResult = result
    }
    static func setPickerCompletionHandler(int: Int) {
        Global.pickerCompletionHandler = int
    }
    static func setProfileCountry(string: String)
    {
        Global.profileCountry = string
    }
}

回答1:


I don't know what all that Global stuff is, the easiest solution might be to extend pickerCompletionHandler, but here is another (easy) one:

  • In PickerViewController add a callback passing a String value and no return value

    var callback : ((String) -> Void)?
    

    and call it in exitButton (please, please variable and function names are supposed to start with a lowercase letter)

    @IBAction func exitButton(_ sender: Any) {
        switch Global.pickerCompletionHandler {
    
        case 0:
            callback?(Global.pickerResult ?? "")
            Global.setProfileCountry(string:
                Global.pickerResult ?? "")
        default:
            print("nothing")
    
        }
        self.view.removeFromSuperview()
    }
    
  • In countryPickButton (again: lowercase letter) set the callback and set sender to the real type. The callback sets the title

    @IBAction func countryPickButton(_ sender: UIButton) {
    
        Global.setPickerDataSource(data:["Test","test2","test3"])
        Global.setPickerCompletionHandler(int:0)
        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PickerViewPopUpId") as! PickerViewController
        popOverVC.callback = { value in 
            sender.setTitle(value, for: .normal)
        }
    ...
    


来源:https://stackoverflow.com/questions/54921382/set-title-of-uibutton-from-selected-row-of-uipickerview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!