我準備了一個自定義控制元件來輸入數值,并在我的控制元件加載中添加了一個觀察者來獲取設備方向的變化,如下面的代碼片段所示。
Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), this.DeviceRotated);
上面的行在我的自定義控制元件中導致記憶體泄漏。有誰知道如何解決添加觀察者引起的記憶體泄漏問題。
uj5u.com熱心網友回復:
我通過像在代碼片段中一樣處理 NSObject 解決了這個問題。
private NSObject deviceRotatedObserver;
this.deviceRotatedObserver = Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), this.DeviceRotated);
protected override void Dispose(bool disposing)
{
if (this.deviceRotatedObserver != null)
{
this.deviceRotatedObserver.Dispose();
this.deviceRotatedObserver = null;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483547.html
