Swift 3 #选择器和类型名称
Swift 3 #selector and type names
刚刚更新为 Xcode8 Swift 3. 不幸的是,我无法将目标添加到目标指向另一个 class 中的函数的按钮。
class AddToDoViewController {
@IBOutlet weak var doneButton: UIButton!
var st : SelectorTest!
override func viewDidLoad() {
st = SelectorTest()
st.didTapButtonHandler = {
self.doneButton.isSelected = !self.doneButton.isSelected
print("self.doneButton.selected=\(self.doneButton.isSelected)")
}
doneButton.addTarget(self, action: #selector(SelectorTest.didTapTheButton(_:)), for: .touchUpInside)
}
}
class SelectorTest {
typealias ToDoCellDidTapButtonHandler = () -> Void
var didTapButtonHandler: ToDoCellDidTapButtonHandler?
@objc func didTapTheButton(_ sender: AnyObject) {
if let handler = didTapButtonHandler {
handler()
}
}
}
在这种情况下,目标是存在于 SelectorTest class 中的 SelectorTest.didTapButton(_:)。但是,当我 运行 并单击按钮时,出现 运行 时间错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[Done.AddToDoViewController didTapTheButton:]:
unrecognized selector sent to instance
由于我将按钮上的目标配置为指向
SelectorTest.didTapTheButton(_:)
这肯定存在,为什么我收到 运行 时间错误,指出它在 AddToDoViewController 上找不到 didTapTheButton(_:)
?特别是当我没有指定 AddToDoViewController.didTapTheButton(_:)
作为目标时?
Done.AddToDoViewController didTapTheButton:
我想知道 #selector
是否忽略了 class 名称并假定目标位于将目标添加到按钮 (AddToDoViewController) 的 class 上?
如何将目标设置为 SelectorTest.didTapTheButton(_:)
并在 运行 时间执行 SelectorTest.didTapTheButton(_:)
而不是 运行 时间尝试触发 AddToDoViewController.didTapTheButton(_:)
哪个不是配置的目标并且不存在?
用 addTarget
代替 self
传递 SelectorTest
class 的对象。
doneButton.addTarget(st, action: #selector(SelectorTest.didTapTheButton(_:)), for: .touchUpInside)
在 addTarget
中,第一个参数目标是 Any?
的类型
The target object is the object whose action method is called. If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object.
刚刚更新为 Xcode8 Swift 3. 不幸的是,我无法将目标添加到目标指向另一个 class 中的函数的按钮。
class AddToDoViewController {
@IBOutlet weak var doneButton: UIButton!
var st : SelectorTest!
override func viewDidLoad() {
st = SelectorTest()
st.didTapButtonHandler = {
self.doneButton.isSelected = !self.doneButton.isSelected
print("self.doneButton.selected=\(self.doneButton.isSelected)")
}
doneButton.addTarget(self, action: #selector(SelectorTest.didTapTheButton(_:)), for: .touchUpInside)
}
}
class SelectorTest {
typealias ToDoCellDidTapButtonHandler = () -> Void
var didTapButtonHandler: ToDoCellDidTapButtonHandler?
@objc func didTapTheButton(_ sender: AnyObject) {
if let handler = didTapButtonHandler {
handler()
}
}
}
在这种情况下,目标是存在于 SelectorTest class 中的 SelectorTest.didTapButton(_:)。但是,当我 运行 并单击按钮时,出现 运行 时间错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[Done.AddToDoViewController didTapTheButton:]:
unrecognized selector sent to instance
由于我将按钮上的目标配置为指向
SelectorTest.didTapTheButton(_:)
这肯定存在,为什么我收到 运行 时间错误,指出它在 AddToDoViewController 上找不到 didTapTheButton(_:)
?特别是当我没有指定 AddToDoViewController.didTapTheButton(_:)
作为目标时?
Done.AddToDoViewController didTapTheButton:
我想知道 #selector
是否忽略了 class 名称并假定目标位于将目标添加到按钮 (AddToDoViewController) 的 class 上?
如何将目标设置为 SelectorTest.didTapTheButton(_:)
并在 运行 时间执行 SelectorTest.didTapTheButton(_:)
而不是 运行 时间尝试触发 AddToDoViewController.didTapTheButton(_:)
哪个不是配置的目标并且不存在?
用 addTarget
代替 self
传递 SelectorTest
class 的对象。
doneButton.addTarget(st, action: #selector(SelectorTest.didTapTheButton(_:)), for: .touchUpInside)
在 addTarget
中,第一个参数目标是 Any?
The target object is the object whose action method is called. If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object.