Thank you to watch my question.
I am writing some code for chose meal by this app.
My idea is this : I call arc4random_uniform method and it will return 5 numbers ,and I just need to set up what every numbers present what meals and show by chosenMeal.text. But It can't work. Can anyone tell me what's wrong? And If you have any code that don't know what I did, please tell me, thank you!
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var meals: UILabel!// not really important
@IBOutlet weak var chosenMeal: UILabel!//This label is used to the page to show detail about what meal that app recommended,
while I push func(checkMeals) that the compiler get me an error : "fatal error: unexpectedly found nil while unwrapping an Optional value"
@IBOutlet weak var pickerView: UIPickerView! // used to chose breakfast or lunch
@IBAction func checkMeals(_ sender: UIButton) {
breakFast = [ "sandwich","Hamburgers", "beef" ,"vegetable", "egg"]
if meals.text == "breakfast" {
let breakFast = arc4random_uniform(5) //
switch breakFast {
case 0:
chosenMeal.text = "sandwich"
case 1:
chosenMeal.text = "Hamburgers"
case 2:
self.chosenMeal.text = "beef"
case 3:
chosenMeal.text = "vegetable"
case 4:
chosenMeal.text = "egg"
default:
break
}
} else if meals.text == "lunch" {
let lunch = (arc4random_uniform(5)+5)
switch lunch {
case 6:
chosenMeal.text = "some1"
case 7:
chosenMeal.text = "some2"
case 8:
chosenMeal.text = "some3"
case 9:
chosenMeal.text = "some4"
case 10:
chosenMeal.text = "some5"
default:
break
}
let foods = ["breakfast", "lunch"]
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return foods[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return foods.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
meals.text = foods[row]
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Aucun commentaire:
Enregistrer un commentaire