我已经在视图控制器上有一个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
}


}

崩溃数据如下:

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:]:此类不符合键选择器View2的键值编码。”
*首先抛出调用堆栈:
(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类型的未捕获异常终止

最佳答案

我只是创建了自己的项目,并将其与您的代码进行了比较,除非我缺少一些非常小的错字,否则您所做的一切都是正确的。这使我认为您的x代码项目出现故障,或者情节提要板插座不再连接都存在问题。

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

如果那不起作用,请检查情节提要出口。当您控制在情节提要板中的pickerView2上单击时,它将向您显示其连接到的插座。

在这种情况下,应该只有一个参考出口,并且一直到其右边的圆圈都应该用白色填充(如下图所示)。

如果没有用白色填充,请删除它,因为它会导致崩溃。然后删除代码出口。然后重新创建插座。

swift - 多 View  Controller 上的多个PickerView-LMLPHP

看来您在做所有正确的代码明智的事情。因此,如果清理后它仍然很糟糕,并且插座正确连接,请删除选择器和所有引用它的代码,然后重新创建它。

让我知道这一切是否仍然对您不起作用。如果没有,请像我一样在界面生成器中为您的情节提要/视图控制器截图。

关于swift - 多 View Controller 上的多个PickerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40877610/

10-09 21:37