Swift 3 - SIGABRT错误 - numberOfComponentsInPickerView:]:无法识别的选择器发送到实例

时间:2016-08-02 18:37:00

标签: swift xcode uipickerview sigabrt

刚刚更新到Swift 3和xCode 8 Beta 4.

修复所有代码问题后,我现在有了一个无错误的项目,但是当点击我的前面屏幕上的一个特定按钮时,它会抛出一个SIGABRT错误。

我确信它与目标页面上的UIPickerView元素有关,因为我已经删除,检查并重新添加了所有的segue和outlet,以确保从那一方清楚一切。

控制台显示以下内容:

  

2016-08-02 18:59:46.607 ForceIt![38735:2895259]    - [ForceIt_.DirectoryViewController numberOfComponentsInPickerView:]:无法识别的选择器发送到实例0x7fcd68c0c210 2016-08-02   18:59:46.618 ForceIt![38735:2895259] ***由于终止应用程序   未捕获的异常'NSInvalidArgumentException',原因:   ' - [ForceIt_.DirectoryViewController numberOfComponentsInPickerView:]:   无法识别的选择器发送到实例0x7fcd68c0c210'

相关viewcontroller的代码如下:

import UIKit

var forceSelectedForTabView = String()
var forceSelectedPositionInArray = Int()

class DirectoryViewController: UIViewController, UIPickerViewDelegate {


    @IBOutlet weak var forcePicker: UIPickerView!
    @IBOutlet weak var selectedContactLabel: UILabel!
    @IBOutlet weak var selectedPhoneTextView: UILabel!
    @IBOutlet weak var selectedWebsiteTextView: UILabel!

    //function for the number of columns in the picker
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    //function counting the array to give the number of rows in the picker
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return forcePickerData.count
    }

    //function displaying the array rows in the picker as a string
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return forcePickerData[row]

    }

    //function allowing the font and colour of the picker to be changed
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let titleData = forcePickerData[row]
        let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Verdana", size: 25.0)!,NSForegroundColorAttributeName:UIColor.black])
        return myTitle
    }

    //function returning the selected row from the picker
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        forceSelectedForTabView = String(forcePickerData[row])
        forceSelectedPositionInArray = forcePickerData.index(of: forcePickerData[row])!


        self.selectedContactLabel.text = issiData[Int(forcePickerData.index(of: forcePickerData[row])!)]


        self.selectedPhoneTextView.text = phoneData[Int(forcePickerData.index(of: forcePickerData[row])!)]


        self.selectedWebsiteTextView.text = websiteData[Int(forcePickerData.index(of: forcePickerData[row])!)]

    }
}

2 个答案:

答案 0 :(得分:7)

numberOfComponentsInPickerView:UIPickerViewDataSource中声明的方法,但您的DirectoryViewController在其符合协议列表中缺少UIPickerViewDataSource

将类标题更改为:

class DirectoryViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

如果没有,Swift无法从方法numberOfComponentsInPickerView:公开numberOfComponents(in:)

答案 1 :(得分:1)

我已经设法通过删除所有以前的代码并从代码库再次插入方法来解决此问题。新代码没有什么不同,但似乎已被接受。

一切都很好 - 猜测我可能刚刚在更新过程中激怒了一个错误。