注意:我自学自学。过去几天我一直在和Swift一起工作。我有一个应用程序教我基础知识。
好的,所以我一直在阅读,我发现了两个可能的原因导致此错误。 1.我的方法/功能中没有代码吗? 我错过了吗
所以它只是突出显示:
class ViewController: UIViewController, UIPickerViewDelegate
它还在ViewController中的V下放置一个小红色箭头。然后错误说明:
在“查看控制器”声明中
我不确定还有什么可能。唯一不属于函数的代码是数组声明。我把这些放在@IBOutlets之后,然后在覆盖func之前。其他所有内容都在覆盖函数或函数中。
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate
{
// connects the text fields
@IBOutlet var raceViewer: UITextField!
@IBOutlet var strStat: UITextField!
@IBOutlet var dexStat: UITextField!
@IBOutlet var conStat: UITextField!
@IBOutlet var intStat: UITextField!
@IBOutlet var wisStat: UITextField!
@IBOutlet var chrStat: UITextField!
// connects stats for human, half-elf, half-orc
@IBOutlet var singleStat: UITextField!
// connects the picker views
@IBOutlet var racePicker: UIPickerView!
@IBOutlet var statPicker: UIPickerView!
@IBOutlet var singleStatPicker: UIPickerView!
// connects the labels for the race modifications
@IBOutlet var raceStr: UILabel!
@IBOutlet var raceDex: UILabel!
@IBOutlet var raceCon: UILabel!
@IBOutlet var raceInt: UILabel!
@IBOutlet var raceWis: UILabel!
@IBOutlet var raceChr: UILabel!
// connects the labels for final modification
// connects the labels for mod
// defines the arrays
var races = ["","Dwarf","Elf","Gnome","Half-Elf","Half-Orc","Halfling","Human","Ifrit","Oread","Sylph","Tengu","Undine","Kitsune","Nagaji","Wayang"]
var abilStats = ["","7","8","9","10","11","12","13","14","15","16","17","18"]
var single = ["Strength","Dexterity","Constitution","Intelligence","Wisdom","Charisma"]
override func viewDidLoad()
{
super.viewDidLoad()
racePicker = UIPickerView()
statPicker = UIPickerView()
singleStatPicker = UIPickerView()
racePicker.delegate = self
statPicker.delegate = self
singleStatPicker.delegate = self
self.raceViewer.inputView = self.racePicker
self.singleStat.inputView = self.singleStatPicker
self.strStat.inputView = self.statPicker
self.dexStat.inputView = self.statPicker
self.conStat.inputView = self.statPicker
self.intStat.inputView = self.statPicker
self.wisStat.inputView = self.statPicker
self.chrStat.inputView = self.statPicker
// set the tags for the pickerview
racePicker.tag = 0
statPicker.tag = 1
singleStatPicker.tag = 2
// Do any additional setup after loading the view, typically from a nib.
//let racePickerView = UIPickerView()
//racePickerView.delegate = self
//raceViewer.inputView = racePicker
singleStat.isHidden = true
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(_ pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if pickerView.tag == 0
{
return races.count
}
else if pickerView.tag == 1
{
return abilStats.count
}
else if pickerView.tag == 2
{
return single.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if pickerView.tag == 0
{
return races[row]
}
else if pickerView.tag == 1
{
return abilStats[row]
}
else if pickerView.tag == 2
{
return single[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView.tag == 0
{
raceViewer.text = races[row]
// Dwarf
if raceViewer.text == races[1]
{
// values changed
raceCon.text = "2"
raceWis.text = "2"
raceChr.text = "-2"
// values reset
raceStr.text = "0"
raceDex.text = "0"
raceInt.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Elf
else if raceViewer.text == races[2]
{
// values changed
raceDex.text = "2"
raceCon.text = "-2"
raceInt.text = "2"
// values reset
raceStr.text = "0"
raceWis.text = "0"
raceChr.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Gnome
else if raceViewer.text == races[3]
{
// values changed
raceStr.text = "-2"
raceCon.text = "2"
raceChr.text = "2"
// values reset
raceDex.text = "0"
raceInt.text = "0"
raceWis.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Half-Elf
else if raceViewer.text == races[4]
{
// values reset
raceStr.text = "0"
raceDex.text = "0"
raceCon.text = "0"
raceInt.text = "0"
raceWis.text = "0"
raceChr.text = "0"
// shows the singleStat text field
singleStat.isHidden = false
}
// Half-Orc
else if raceViewer.text == races[5]
{
// values reset
raceStr.text = "0"
raceDex.text = "0"
raceCon.text = "0"
raceInt.text = "0"
raceWis.text = "0"
raceChr.text = "0"
// shows the singleStat text field
singleStat.isHidden = false
}
// Halfling
else if raceViewer.text == races[6]
{
// values changed
raceStr.text = "-2"
raceDex.text = "2"
raceChr.text = "2"
// values reset
raceCon.text = "0"
raceInt.text = "0"
raceWis.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Human
else if raceViewer.text == races[7]
{
// values reset
raceStr.text = "0"
raceDex.text = "0"
raceCon.text = "0"
raceInt.text = "0"
raceWis.text = "0"
raceChr.text = "0"
// shows the singleStat text field
singleStat.isHidden = false
}
// Ifrit
else if raceViewer.text == races[8]
{
// values changed
raceDex.text = "2"
raceWis.text = "-2"
raceChr.text = "2"
// values reset
raceStr.text = "0"
raceCon.text = "0"
raceInt.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Oread
else if raceViewer.text == races[9]
{
// values changed
raceStr.text = "2"
raceWis.text = "2"
raceChr.text = "-2"
// values reset
raceDex.text = "0"
raceCon.text = "0"
raceInt.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Sylph
else if raceViewer.text == races[10]
{
// values changed
raceDex.text = "2"
raceCon.text = "-2"
raceInt.text = "2"
// values reset
raceStr.text = "0"
raceWis.text = "0"
raceChr.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Tengu
else if raceViewer.text == races[11]
{
// values changed
raceStr.text = "-2"
raceDex.text = "2"
raceWis.text = "2"
// values reset
raceCon.text = "0"
raceInt.text = "0"
raceChr.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Undine
else if raceViewer.text == races[12]
{
// values changed
raceStr.text = "-2"
raceDex.text = "2"
raceWis.text = "2"
// values reset
raceCon.text = "0"
raceInt.text = "0"
raceChr.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Kitsune
else if raceViewer.text == races[13]
{
// values changed
raceStr.text = "-2"
raceDex.text = "2"
raceChr.text = "2"
// values reset
raceCon.text = "0"
raceInt.text = "0"
raceWis.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Nagagi
else if raceViewer.text == races[14]
{
// values changed
raceStr.text = "2"
raceInt.text = "-2"
raceChr.text = "2"
// values reset
raceDex.text = "0"
raceCon.text = "0"
raceWis.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
// Wayang
else if raceViewer.text == races[15]
{
// values changed
raceDex.text = "2"
raceInt.text = "2"
raceWis.text = "-2"
// values reset
raceStr.text = "0"
raceCon.text = "0"
raceChr.text = "0"
// hides the singleStat text field
singleStat.isHidden = true
}
}
}
}