Random Swift3中的随机选择和UILabel
我正在为这个应用程序选择的膳食编写一些代码 我的想法是:我调用arc4random_统一方法,它将返回5个数字,我只需要设置每个数字表示哪些食物,并通过chosenmeat.text显示。谁能告诉我怎么了? 如果你有不知道我做了什么的代码Random Swift3中的随机选择和UILabel,random,uilabel,Random,Uilabel,我正在为这个应用程序选择的膳食编写一些代码 我的想法是:我调用arc4random_统一方法,它将返回5个数字,我只需要设置每个数字表示哪些食物,并通过chosenmeat.text显示。谁能告诉我怎么了? 如果你有不知道我做了什么的代码 import UIKit class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { @IBOutlet weak var meals: UI
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.
}
}请确保已在情节提要中正确绑定Chosenmeid UILabel。好的,谢谢。