快速从 UIPickerView 的选定行设置 UIButton 的标题 [英] Set Title of UIButton from Selected Row of UIPickerView in swift
问题描述
我正在尝试从 uipickerview 的选定行设置我的按钮标题.但是,我似乎找不到有效的解决方案.
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.
我尝试过的事情:
- 全局函数(无法工作,因为您无法指定要更改名称的按钮)
- Dispatchqueue.main.async - 没有更新我的标题.这里的想法是让它不断检查字符串的变化.
我在这里找到的解决方案是在 Obj-C 中.我尝试将其转换为 swift https://objectivec2swift.com/#/converter/code/ 没有运气.从 UIPickerView 的选定行设置 UIButton 的标题
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
}
}
推荐答案
我不知道所有 Global
的东西是什么,最简单的解决方案可能是扩展 pickerCompletionHandler
,但这是另一个(简单的):
I don't know what all that Global
stuff is, the easiest solution might be to extend pickerCompletionHandler
, but here is another (easy) one:
在
PickerViewController
中添加一个回调,传递一个String
值并且没有返回值
In
PickerViewController
add a callback passing aString
value and no return value
var callback : ((String) -> Void)?
并在exitButton
中调用它(请注意,变量名和函数名应该以小写字母开头)
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()
}
在 countryPickButton
(再次:小写 字母)中设置回调并将 sender
设置为真实类型.回调设置标题
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)
}
...
这篇关于快速从 UIPickerView 的选定行设置 UIButton 的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!