我有一個weak
屬性爲其超級視圖(視圖B)的視圖(我們稱之爲視圖A)。查看KVO的超級視圖,查看B.由於視圖A對視圖B的引用是一個弱屬性(以防止保留週期),我怎樣才能移除觀察者(A觀察B)?查看A對視圖B的引用在我有機會刪除它之前沒有被刪除。如何從弱財產中清除KVO?
甲會超越乙由於視圖控制器具有強引用A.這裏的泄漏日誌消息:
An instance 0x9ac5200 of class UITableView 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:
<NSKeyValueObservationInfo 0x8660360> (
<NSKeyValueObservance 0x8660320: Observer: 0x8660020, Key path: contentOffset, Options: <New: YES, Old: NO, Prior: NO> Context: 0x8660020, Property: 0x864ac80>
)
B是一個UITableView。在NSKVODeallocateBreak
處設置斷點會產生無用的結果。
在A的removeFromSuperview
中,我嘗試刪除觀察者,但A對B的引用已經是nil
。
切換到unsafe_unretained
並在視圖控制器的dealloc
中手動執行更多操作或調用[A removeFromSuperview]
可解決此問題。我想知道如何使用weak
屬性來解決這個問題。
下面是相關代碼:https://gist.github.com/2822776
我的不好... +1無論如何。 – CodaFi