weak
スーパービュー (ビュー B) へのプロパティを持つビュー (このビュー A と呼びます) があります。ビュー A KVO のスーパービュー、ビュー B。ビュー A のビュー B への参照は弱いプロパティであるため (保持サイクルを防ぐため)、オブザーバー (A が B を観察している) を削除するにはどうすればよいですか? ビュー A のビュー B への参照は、削除する前にゼロになります。
View Controller は A への強力な参照を持っているため、A は B よりも長生きします。これはリーク ログ メッセージです。
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
役に立たない結果が生じます。
AremoveFromSuperview
では、オブザーバーを削除しようとしましたが、A の B への参照は既にnil
です。
切り替えてunsafe_unretained
手動で行うか[A removeFromSuperview]
、ビューコントローラーを呼び出すとdealloc
、問題が解決します。weak
プロパティを使用してこれを解決する方法を知りたいのですが。
関連するコードは次のとおりです: https://gist.github.com/2822776