2017-04-03 112 views
0

我正在尝试开发一个应用程序,现在我想通过输入来编辑标签的文本2个不同的UIPickerViews。线程1:exc_bad_instruction(code = exc_i386_invop,子代码= 0x0)/致命错误:索引超出范围

起初我会得到错误。
"thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0)" 指向的代码(见代码)某一位,但现在我只能得到错误 "fatal error: Index out of range"

的问题是在第一选择器,如果我改变什么在那里(在模拟器!)的应用程序崩溃。如果我只更换第二个选择器,然后更新标签,它将更改为“1天”或“2周”,而不是仅“天”或“周”。

我的故事板:

main.storyboard

我的代码:

import UIKit 

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 

@IBOutlet weak var PickerView1: UIPickerView! 
@IBOutlet weak var PickerView2: UIPickerView! 
@IBOutlet weak var outputLabel: UILabel! 
@IBAction func changeLabel(_ sender: UIButton) { 
    outputLabel.text = outputTotal 
} 

var list1 = [""] 
var list2 = [""] 
var output1 = "" 
var output2 = "" 
var outputTotal = "" 

func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 1 
} 

// Aangeven welke items er in de pickerview komen 
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    if (pickerView.tag == 1){ 
     return "\(list1[row])" 
    } else { 
     return "\(list2[row])" 
    } 
} 

// Aantal rijen weergeven 
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    if (pickerView.tag == 1){ 
     return list1.count 
    } else { 
     return list2.count 
    } 
} 

// Label veranderen naar input van PickerView1 
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    output1 = list1[row] 
    output2 = list2[row] // error points here 
    outputTotal = output1 + " " + output2 
} 

override func viewDidLoad() { 
    list1 = ["1", "2", "3", "4", "5", "6", "7", "8"] 
    list2 = ["days", "weeks"] 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

如果有人能帮助我,谢谢!

回答

1

我想你忘了检测pickerView(_:didSelectRow:inComponent:)其中pickView选择:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    if pickerView.tag == 1 { 
     output1 = list1[row] 
    } else { 
     output2 = list2[row] 
    } 
    outputTotal = output1 + " " + output2 
} 
+0

哦,上帝,这是实际上是非常简单的,现在我觉得自己很蠢,因为我没有看到它..感谢! ^^ –

+0

不客气!并且不要忘记接受我的回答。谢谢! –

相关问题