[英]How to compare selected UIPickerView row element to Array element?
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var categoryFactsPincker: UIPickerView!
@IBOutlet weak var randomFactLabel: UILabel!
This is my array of my UIPickerView 这是我的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
}
I would like to use this button to compare array element and UIPickerView selected for print different categories 我想使用此按钮比较数组元素和选择用于打印不同类别的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]
}
} }
try this 尝试这个
@IBAction func buttonPressed(_ sender: UIButton) {
if categoryFactsPincker.selectedRow(inComponent: 0) == 0 {
updateInterestingFacts()
}else{
let categorySelected = categoryFactsArray[categoryFactsPincker.selectedRow(inComponent: 0)]
print(categorySelected)
}
}
this print should show the selected value, so you can use to compare, or whatever you need 此打印应显示选定的值,以便您可以进行比较或进行任何所需的比较
At the end I fixed it with this code: 最后,我使用以下代码对其进行了修复:
@IBAction func buttonPressed(_ sender: UIButton) {
if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 0 {
updateInterestingFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 1 {
updateFunFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 2{
updateNutritionFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 3{
updateFunnyFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 4{
updateUnbelievableFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 5{
updateDidYouKnowFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 6{
updateAnimalFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 7{
updateMindBlowingFacts()
}else if Int(categoryFactsPincker.selectedRow(inComponent: 0)) == 8{
updateWeirdFacts()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.