我是Swift新手,尝试在我的UIPickerView中使用int值0-10,但有一些错误,我真的不明白。
这是我的代码-我把UIPickerView放在一个TextField中,我只是觉得这样看起来更整洁。
class AddViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var repeatTextField: UITextField!
@IBOutlet weak var startPicker: UIDatePicker!
@IBOutlet weak var endPicker: UIDatePicker!
@IBOutlet weak var doneButton: UIBarButtonItem!
var pickerView = UIPickerView()
var numbers = [String] = []
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
repeatTextField.inputView = pickerView
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
pickerView.delegate = self
pickerView.dataSource = self
for i in 1..<10 {
numbers.append(String(i))
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
internal func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(row + 1)
}
}
这是错误出现的地方:
var numbers = [String] = []
我只能看到使用UIPickerView与字符串的教程,但不能看到整数。
这就是我想要达到的目标:
enter image description here
你能帮我解决这个问题吗?提前谢谢!:))
1条答案
按热度按时间ikfrs5lh1#
您可以:
var numbers: [String] = []
或var numbers = [String]()