我们制作了一个带有选择器视图的电子邮件功能,该视图将其变量写入标签中。这是有效的,现在我们添加了第二个选择器视图,以便对您应该能够发送到该电子邮件地址的文章执行相同操作。
基本上它可以工作但是如果每个选择器视图中的值的数量不同,它会在某些时候因所述错误而崩溃。
以下是代码:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var techniker: UIPickerView!
@IBOutlet weak var artikel: UIPickerView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var label1: UILabel!
var email = ["email1@email.ch", "email2@email.ch", "email3@email.ch", "email4@email.ch", "email5@email.ch"]
var materialien = ["article", "article", "article", "article", "article", "article"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countrows : Int
if pickerView == artikel {
countrows = materialien.count
}
else {
countrows = materialien.count
}
return countrows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == techniker {
return email[row]
}; if pickerView == artikel {
return materialien[row]
} else {
return ""
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == techniker {
label.text = email[row]
}; if pickerView == artikel {
label1.text = materialien[row]
}
}
}
答案 0 :(得分:0)
您的numberOfRowsInComponent
返回materialien
的计数为6.因此,如果您尝试显示email[row]
哪个(电子邮件有5个项目),那么这将超出范围。< / p>
我怀疑您应该将numberOfRowsInComponant
中的部分更改为
if pickerView == artikel {
countrows = email.count
} else {
countrows = materialien.count
}