我正在尝试从uipickerview的选定行设置我的按钮标题。但是,我似乎找不到有效的解决方案。
我将变量设置为全局,因此我可以访问所选变量。但是,我似乎无法确切查明用户单击"出口"按钮并能够更新我的按钮标题的准确指出。
我尝试过的事情:
- 全局函数(因为您无法指定要更改名称的按钮) )
- dispatchqueue.main.async-没有更新我的标题。这里的想法是要不断检查字符串更改。
我在此处找到的解决方案是在OBJ-C中。我尝试将其转换为Swift https://objectivec2swift.com/#/converter/code/没有运气。从uipickerview的选定行
设置uibutton的标题@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
}
}
我不知道Global
的内容是什么,最简单的解决方案可能是扩展pickerCompletionHandler
,但这是另一个(简单):
-
在
PickerViewController
中添加一个回调,传递String
值,没有返回值var callback : ((String) -> Void)?
并在
exitButton
中调用它(请,请变量和功能名称应该以小写字母开头)@IBAction func exitButton(_ sender: Any) { switch Global.pickerCompletionHandler { case 0: callback?(Global.pickerResult ?? "") Global.setProfileCountry(string: Global.pickerResult ?? "") default: print("nothing") } self.view.removeFromSuperview() }
-
在
countryPickButton
中(再次: pericalcase 字母)设置回调并将sender
设置为真实类型。回调设置标题@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) } ...