Error: Unrecognised selector sent to instance when tap on UISegmentedControl
Error: Unrecognised selector sent to instance when tap on UISegmentedControl
点击其中一个片段触发操作时出现错误。
知道我做错了什么吗?
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestingSubclassing.MySegmentControl segmentActionWithSender:]: unrecognized selector sent to instance 0x7f9217907750'
import UIKit
class MySegmentControl: UISegmentedControl {
var actionName:Selector
init(actionName: Selector) {
self.actionName = actionName
super.init(frame: .zero)
insertSegment(withTitle: "Two", at: 0, animated: false)
insertSegment(withTitle: "One", at: 1, animated: false)
self.selectedSegmentIndex = 0
self.layer.cornerRadius = 5.0
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.blue.cgColor
self.addTarget(self, action: self.actionName, for: .valueChanged)
self.translatesAutoresizingMaskIntoConstraints = false
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在ViewController
中的用法
import UIKit
class ViewController: UIViewController {
let segmentOne: MySegmentControl = {
let segment1 = MySegmentControl(actionName: #selector(segmentAction))
return segment1
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(segmentOne)
}
@objc func segmentAction(sender: UISegmentedControl) {
print("segmentAction")
}
}
我也试过如下更改选择器。
let segment1 = MySegmentControl(actionName: #selector(segmentAction(sender:)))
和
let segment1 = MySegmentControl(actionName: "segmentAction")
你一定是新手吧!
您应该在 class MySegmentControl
中删除 self.addTarget(self, action: self.actionName, for: .valueChanged)
,添加下面的函数 viewDidLoad()
:
segmentOne.addTarget(self, action: #selector(segmentAction(sender:)), for: .valueChanged)
点击其中一个片段触发操作时出现错误。
知道我做错了什么吗?
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestingSubclassing.MySegmentControl segmentActionWithSender:]: unrecognized selector sent to instance 0x7f9217907750'
import UIKit
class MySegmentControl: UISegmentedControl {
var actionName:Selector
init(actionName: Selector) {
self.actionName = actionName
super.init(frame: .zero)
insertSegment(withTitle: "Two", at: 0, animated: false)
insertSegment(withTitle: "One", at: 1, animated: false)
self.selectedSegmentIndex = 0
self.layer.cornerRadius = 5.0
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.blue.cgColor
self.addTarget(self, action: self.actionName, for: .valueChanged)
self.translatesAutoresizingMaskIntoConstraints = false
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在ViewController
中的用法import UIKit
class ViewController: UIViewController {
let segmentOne: MySegmentControl = {
let segment1 = MySegmentControl(actionName: #selector(segmentAction))
return segment1
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(segmentOne)
}
@objc func segmentAction(sender: UISegmentedControl) {
print("segmentAction")
}
}
我也试过如下更改选择器。
let segment1 = MySegmentControl(actionName: #selector(segmentAction(sender:)))
和
let segment1 = MySegmentControl(actionName: "segmentAction")
你一定是新手吧!
您应该在 class MySegmentControl
中删除 self.addTarget(self, action: self.actionName, for: .valueChanged)
,添加下面的函数 viewDidLoad()
:
segmentOne.addTarget(self, action: #selector(segmentAction(sender:)), for: .valueChanged)