我想要的是让用户滚动选择器以及用户在标签处停留的位置具有选择器中项目的描述。到目前为止,我已将所有代码都包括在内。我在iOS上只有大约2个月的Swift编码,所以我相信我在这里缺少很多概念。谢谢你的时间。
import UIKit
class Weapons: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var WeaponPicker: UIPickerView!
@IBOutlet weak var WeaponLabel: UILabel!
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
WeaponPicker.delegate = self
pickerData = ["Great Sword", "Switch Axe", "Hammer", "Lance", "Bow", "Light Bowgun"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
}
答案 0 :(得分:1)
实施didSelectRow
然后你可以做类似
的事情WeaponLabel.text = pickerData[row]
编辑:添加解释。
如果您想根据武器显示说明,可以这样做:
制作武器和描述的结构:
struct weaponObject {
var name: String
var description: String
}
声明一个武器对象类型的数组:
var pickerData = [weaponObject]()
在viewDidLoad
:
let names = ["name1", "name2"]
let descriptions = ["description1", "description2"]
for (name, description) in zip(names, descriptions) {
pickerData.append(weaponObject(name: name, description: description))
}
在didSelectRow
:
WeaponLabel.text = pickerData[row].description
在titleForRow
:
return pickerData[row].name