2017-04-03 111 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

不客氣!並且不要忘記接受我的回答。謝謝! –

相關問題