我正在向我的視圖控制器添加一個名爲bookViewContainer
的UIView,並且我想要檢測其規模何時使用KVO進行更改。這裏是我的viewDidLoad
:在UIView上創建KVO時出錯
- (void)viewDidLoad
{
[super viewDidLoad];
bookViewContainer = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:bookViewContainer];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[bookViewContainer addGestureRecognizer:tap];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRecognized:)];
[bookViewContainer addGestureRecognizer:pinch];
[bookViewContainer addObserver:self forKeyPath:@"transform.scale" options:NSKeyValueObservingOptionNew context:NULL];
}
和我observeValueForKeyPath
:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@", object);
}
然而,當我運行該項目,我立刻得到錯誤:
An instance 0x1d844ca0 of class NSConcreteValue was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: ( Context: 0x0, Property: 0x1d844db0> )
我用志願前(但從來沒有在transform.scale
),並且該視圖絕對不會被釋放(我正在使用ARC)。我試過用scale
代替,但沒有任何反應。我究竟做錯了什麼?