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]
}
}