无法识别的选择器使用pickerView发送到实例

时间:2018-10-26 01:23:09

标签: swift uipickerview

2018-10-25 18:19:37.940317-0700 AddressBookApp[3976:92391] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
2018-10-25 18:19:47.916807-0700 AddressBookApp[3976:92391] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/broque/Library/Developer/CoreSimulator/Devices/A4672181-9DB1-4F6B-8251-1EFB52A087A5/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-10-25 18:19:47.918140-0700 AddressBookApp[3976:92391] [MC] Reading from private effective user settings.
2018-10-25 18:20:01.956143-0700 AddressBookApp[3976:92391] -[AddressBookApp.SecondViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7f9007409f60
2018-10-25 18:20:01.971135-0700 AddressBookApp[3976:92391] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AddressBookApp.SecondViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7f9007409f60'
import UIKit

class SecondViewController: UIViewController {
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var number: UILabel!
    @IBOutlet weak var email: UILabel!

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return (FirstViewController.model.sendArray(index: row-1)?.lastName)! + ", " + (FirstViewController.model.sendArray(index: row-1)?.firstName)!
    }

    // called when a selection is made
    // recall that the first row is row 0
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        name.text = (FirstViewController.model.sendArray(index: row-1)?.firstName)! + ", " + (FirstViewController.model.sendArray(index: row-1)?.lastName)!
        number.text = FirstViewController.model.sendArray(index: row-1)?.number
        email.text = FirstViewController.model.sendArray(index: row-1)?.email
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent
        component: Int) -> Int {
        return 5 // this should be declared as a constant!
    }
}

1 个答案:

答案 0 :(得分:0)

不要自己完全键入方法签名。让Xcode为您完成它。

应该是:

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

您还应该指出您的视图控制器符合选择器视图协议:

class SecondViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {