我在ios应用程序中的UIpickerView上添加了取消和完成按钮,但我收到错误 - [Avakaash.Profile_add_update donePicker ]:无法识别的选择器发送到实例0x7faf19daf1f0' ,我的代码如下所示 -
override func viewDidLoad() {
super.viewDidLoad()
std_class_pickerView.tag = 0
country_pickerview.tag = 1
std_class_pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
std_class_pickerView.backgroundColor = .whiteColor()
std_class_pickerView.showsSelectionIndicator = true
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("donePicker"))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("canclePicker"))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
std_class_pickerView.delegate = self
text_std_class.inputView = std_class_pickerView
text_std_class.inputAccessoryView = toolBar
}
我的控制器类看起来像 -
class Profile_add_update: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
}
错误日志 -
[Avakaash.Profile_add_update donePicker]: unrecognized selector sent to instance 0x7faf19daf1f0
2016-08-18 17:04:13.653 Avakaash[2999:227329] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'- [Avakaash.Profile_add_update donePicker]: unrecognized selector
sent to instance 0x7faf19daf1f0'
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:1)
您忘记实施donePicker
和canclePicker
按钮的操作
func donePicker () //Create an IBAction
{
// do something
}
func canclePicker () //Create an IBAction
{
// do something
}
完整代码
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style:.Plain, target: self, action: #selector(ViewController.donePicker))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style:.Plain, target: self, action: #selector(ViewController.canclePicker))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
像这样的行动
func donePicker () //Create an IBAction
{
// do something
}
func canclePicker () //Create an IBAction
{
// do something
}
答案 1 :(得分:0)
您似乎已将此问题(Add buttons to UIPickerView - Swift 1.2)的答案剪切并粘贴到您的应用程序中,而不了解其工作原理。这不是学习如何编写代码的好方法。
错误消息告诉您正在尝试调用名为“donePicker”的选择器(或函数或操作),但不存在此类选择器。您需要在名为“donePicker”的类中创建一个操作(还有一个名为“canclePicker”(sic。)),以使您的代码能够正常工作。