gpt4 book ai didi

ios - 如何将选定的 UIPickerView 行元素与数组元素进行比较?

转载 作者:行者123 更新时间:2023-11-28 07:29:51 25 4
gpt4 key购买 nike

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


@IBOutlet weak var categoryFactsPincker: UIPickerView!

@IBOutlet weak var randomFactLabel: UILabel!

这是我的 UIPickerView 数组

let categoryFactsArray = ["Interesting Facts", "Fun Facts", "Nutrition Facts", "Funny Facts", "Unbelievable Facts", "Did You Know", "Animal Facts", "Mind Blowing Facts", "Weird Facts"]


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

categoryFactsPincker.delegate = self
categoryFactsPincker.dataSource = self
}

我想用这个按钮来比较数组元素和选择用于打印不同类别的 UIPickerView

@IBAction func buttonPressed(_ sender: UIButton) {

if categoryFactsArray[row] == 0 {

updateInterestingFacts()
}else if categoryFactsPincker.selectedRow(inComponent: 1){

}
}

func updateInterestingFacts(){

let random = interestingFactsArray[Int(arc4random_uniform(UInt32(interestingFactsArray.count)))]

// randomFactLabel.text = random

let alert = UIAlertController(title: "Random Fact", message: "\(random)", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)

}

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

return categoryFactsArray.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

return categoryFactsArray[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

categoryFactsArray[row]
}

最佳答案

试试这个

        @IBAction func buttonPressed(_ sender: UIButton) {

if categoryFactsPincker.selectedRow(inComponent: 0) == 0 {
updateInterestingFacts()
}else{
let categorySelected = categoryFactsArray[categoryFactsPincker.selectedRow(inComponent: 0)]
print(categorySelected)
}
}

这个打印应该显示选择的值,所以你可以用来比较,或者任何你需要的

关于ios - 如何将选定的 UIPickerView 行元素与数组元素进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com