在 swift 中从 UIPickerView 的选定行设置 UIButton 的标题
Set Title of UIButton from Selected Row of UIPickerView in swift
我正在尝试从 uipickerview 的选定行设置我的按钮标题。但是,我似乎找不到有效的解决方案。
我已将变量设置为全局变量,因此我可以访问所选变量。但是我似乎无法准确指出用户何时单击退出按钮并能够更新我的按钮标题。
我尝试过的事情:
- 全局函数(无法工作,因为您无法指定要更改名称的按钮)
- Dispatchqueue.main.async - 没有更新我的标题。这里的想法是让它不断检查字符串变化。
我在这里找到的解决方案在 Obj-C 中。我尝试将其转换为 swift https://objectivec2swift.com/#/converter/code/ 但没有成功。
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]
}
}
全局变量
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
值且没有 return 值的回调
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
中(再次:小写字母)设置回调并将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)
}
...
我正在尝试从 uipickerview 的选定行设置我的按钮标题。但是,我似乎找不到有效的解决方案。
我已将变量设置为全局变量,因此我可以访问所选变量。但是我似乎无法准确指出用户何时单击退出按钮并能够更新我的按钮标题。
我尝试过的事情:
- 全局函数(无法工作,因为您无法指定要更改名称的按钮)
- Dispatchqueue.main.async - 没有更新我的标题。这里的想法是让它不断检查字符串变化。
我在这里找到的解决方案在 Obj-C 中。我尝试将其转换为 swift https://objectivec2swift.com/#/converter/code/ 但没有成功。 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]
}
}
全局变量
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
值且没有 return 值的回调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
中(再次:小写字母)设置回调并将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) } ...