多视图控制器上的多个PickerView

时间:2016-11-29 23:22:26

标签: swift swift3

我已经在viewcontroller上有一个pickerview然后我有一个新的viewcontroller我试图输入一个新的pickerview但是每次我测试我的代码时我都会收到SIGABRT错误。我知道这必须与我已经使用这些功能的事实有关,我不知道该怎么做。任何正确方向的帮助都会很好。

由于

class ViewController2: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet var pickerView2: UIPickerView!

var aparray = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"]



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    pickerView2.delegate = self
   pickerView2.dataSource = self


}

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




func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return aparray[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return aparray.count
}

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

}

类ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

@IBOutlet var pickerView: UIPickerView!

var array = ["1","2","3","4","5","6"]



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    pickerView.delegate = self
   pickerView.dataSource = self


}

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




func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return array[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return array.count
}

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

}

以下CRASH数据:

2016-11-29 20:12:19.441114 testapp[1950:449604] Unknown class Vc2 in Interface Builder file.

2016-11-29 20:12:19.443065 testapp [1950:449604] *由于未捕获的异常终止应用' NSUnknownKeyException',原因:' [setValue:forUndefinedKey:] :此类不是键值pickerView2的键值编码兼容。' * 第一次抛出调用堆栈: (0x193a7e1c0 0x1924b855c 0x193a7de88 0x194488110 0x199bda758 0x199ddc0c8 0x19399ddc4 0x199ddaa9c 0x199c3b444 0x199a0d53c 0x1998d8d48 0x1998d8c78 0x1998df424 0x1998dc8c4 0x19994f0e8 0x199b5ba78 0x199b615c8 0x199b75e60 0x199b5e5ac 0x1956258bc 0x195625728 0x195625ad0 0x193a2c278 0x193a2bbc0 0x193a297c0 0x193958048 0x1999442b0 0x19993f034 0x1000fac24 0x19293c5b8) libc ++ abi.dylib:以NSException类型的未捕获异常终止

1 个答案:

答案 0 :(得分:0)

我刚刚制作了自己的项目,并将其与您的代码进行了比较,除非我错过了一些非常小的错字,否则您正在做正确的事情。这让我觉得你的x代码项目有问题,或者故事板插座不再连接存在问题。

尝试完全退出Xcode然后重新打开,单击产品然后清除。

如果这不起作用,请检查故事板插座。当您控制在故事板中单击pickerView2时,它应该显示它所连接的出口。

在这种情况下,应该只有一个Referencing Outlet,它右边的圆圈应该用白色填充(如下图所示)。

如果未填充白色,请将其删除,因为它会导致崩溃。然后删除代码插座。然后重新创建插座。

enter image description here

看起来你正在做一切正确的代码。因此,如果在清理后继续糟糕,并且插座正确连接,请删除选择器和引用它的所有代码,然后重新创建它。

如果所有这些仍然不适合您,请告诉我。如果它没有,请像我一样在界面构建器中截取您的storyboard / viewcontrollers。