[英]Thread 1: Fatal error: Index out of range on swift 4
I have a issue on Thread 1 fatal error on my code.我的代码中的线程 1 致命错误有问题。 I try to create 2 pickers in one view controller.
我尝试在一个视图控制器中创建 2 个选择器。 But the error keep coming.
但错误不断出现。 When I tried to run on my phone, everything is okay till this page (picker cannot move).
当我尝试在手机上运行时,在此页面之前一切正常(选择器无法移动)。 It shows an error on (return subject[row]) on 6 line from bottom)
它在从底部开始的第 6 行上显示 (return subject[row]) 上的错误)
enter image description here在此处输入图片说明
Heres my code:这是我的代码:
Import UIKit
class ThirdViewController: UIViewController, UIPickerViewDelegate,
UIPickerViewDataSource {
@IBOutlet weak var classPickerView: UIPickerView!
@IBOutlet weak var subjectPickerView: UIPickerView!
@IBOutlet weak var nextButton: UIButton!
let subclass = ["AA234", "ASA231", "AA9292", "AAA839", "AA5682", "AAA789"]
let subject = ["Introduction to Database","Introduction to Programming","Mathematics","Multimedia"]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonPressed(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = (self as! UIImagePickerControllerDelegate & UINavigationControllerDelegate)
//select row first
let selectedRow1 = classPickerView.selectedRow(inComponent: 0)
let selectedRow2 = subjectPickerView.selectedRow(inComponent: 0)
let selectedClass = subclass [selectedRow1]
let selectedSubClass = subject [selectedRow2]
let messageToShow = "Class \(selectedClass) with the subject \(selectedSubClass)"
// insert messageToShow in message to appear what u selected
let actionSheet = UIAlertController (title: "Please Confirm Before Scan", message: messageToShow, preferredStyle: .alert)
let okAction = UIAlertAction (title: "Proceed to Scan", style: .default, handler: nil) //{action in
//picker.sourceType = .camera})
let cancelAction = UIAlertAction (title: "Cancel", style: .cancel, handler: nil)
actionSheet.addAction(okAction)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true, completion: nil)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 1) {
return subject.count
}
else {
return subclass.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return subject[row]
}
else {
return subclass[row]
}
}
} }
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (component == 0) {
return subject.count
}
else {
return subclass.count
}
}
Change numberOfRowsInComponent code as above更改 numberOfRowsInComponent 代码如上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.