Ios Swift中对象不符合kvc时的对象通信
我想创建一个自定义视图,在另一个对象上发生动作(如更改的属性)时显示它(UIControl的子类) 我的方法就是这样Ios Swift中对象不符合kvc时的对象通信,ios,swift,protocols,key-value-observing,cocoa-design-patterns,Ios,Swift,Protocols,Key Value Observing,Cocoa Design Patterns,我想创建一个自定义视图,在另一个对象上发生动作(如更改的属性)时显示它(UIControl的子类) 我的方法就是这样 创建其UIControl对象可以符合的协议 创建我的自定义视图,在其中我可以观察我的委托 不幸的是,它不工作,更糟糕的是,它崩溃了,因为编译器说“它不符合kvc” 下面,我的代码: protocol CustomDelegate: class where Self : UIControl { func respondeToControl() -> Bool } cl
下面,我的代码:
protocol CustomDelegate: class where Self : UIControl {
func respondeToControl() -> Bool
}
class CustomView: UIView {
weak var delegate: CustomDelegate? {
didSet{
observeIfControlIsPressed()
}
}
func observeIfControlIsPressed(){
if delegate != nil {
let keypath = delegate! as! UIControl
keypath.addObserver(keypath,
forKeyPath: "backgroundColor",
options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.old],
context: nil)
}
}
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("background changed")
}
}
我的问题是,我如何重新设计我的解决方案来实现它
工作?您添加的观察者是错误的,“观察者”应该是self无“关键路径” 试试这个:
keypath.addObserver(self, forKeyPath: "backgroundColor", options: [.new, .old], context: nil)
您没有正确读取错误。。例如:
protocol CustomDelegate : class where Self : UIControl {
func respondeToControl() -> Bool
}
class CustomView: UIView {
weak var delegate: CustomDelegate? {
didSet{
observeIfControlIsPressed()
}
}
func observeIfControlIsPressed(){
if delegate != nil {
let keypath = delegate! as! UIControl
keypath.addObserver(keypath,
forKeyPath: "backgroundColor",
options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.old],
context: nil)
}
}
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("background changed")
}
}
class Foo : UIControl, CustomDelegate {
func respondeToControl() -> Bool {
return true
}
}
class ViewController: UIViewController {
let testBlock = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let view = CustomView()
let button = Foo()
view.delegate = button
button.backgroundColor = UIColor.red
}
}
引发异常:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<Meh.Foo: 0x7fc977c1a0c0; baseClass = UIControl; frame = (0 0; 0 0); layer = <CALayer: 0x608000226480>>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: backgroundColor
其中,keypath
是您的代理人。。并且在自己身上添加了观察者
应该是:
keypath.addObserver(self, //self is observing keypath..
forKeyPath: "backgroundColor",
options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.old],
context: nil)
addObserver
的第一个参数必须是要进行观察的类
该更改将导致打印:
背景已更改
。。以后别忘了删除观察者。非常感谢您花时间理解并解决我的问题+1.
keypath.addObserver(self, //self is observing keypath..
forKeyPath: "backgroundColor",
options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.old],
context: nil)