2017-04-03 9 views
0

私はアプリケーションを開発しようとしていますが、今のところ私はラベルのテキストを2種類のUIPickerViews。スレッド1:exc_bad_instruction(code = exc_i386_invop、subcode = 0x0)/致命的なエラー:インデックスが範囲外です

最初はエラーが発生します。
"thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0)" コード(参照コード)の特定のビットを指しますが、今私は、エラー "fatal error: Index out of range"

私はそこに何かを変更した場合、問題は、最初のピッカーである(シミュレータで!)アプリのクラッシュを取得。 2番目のピッカーのみを変更してラベルを更新すると、ラベルは 'days'または 'weeks'の代わりに '1 days'または '2 weeks'に変更されます。

マイストーリーボード:

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

よろしくお願いします!私の答えを受け入れることを忘れないでください。ありがとう! –

関連する問題