Reputation: 33058
The question's title may sound stupid, but I have beenw wondering about the following issue:
UIView
to the app's window. It will not rotate, because there is no controller which implements ShouldAutoRotateToInterfaceOrientation
(): window.AddSubView(myView)
;LoadView()
override ShouldAutoRotateToInterfaceOrientation()
. Then do this: window.AddSubView(myController.View);
In the latter case the view rotates as expected. Why? Because its view controller tells it to do so. But how does the view know which controller it belongs to? I did not tell it! From the ObjC people I learned that each view has a "_viewDelegate
" property which refers back to the view's controller.
But if that is true, why do I then have to keep a reference of myController, best in a member variable, to prevent it from being Garbage Collected? My test case in AppDelegate.cs:
public static AddView()
{
var myController = new MyController();
window.AddSubView(myController.View);
}
The view will be correct first time, then the controller seems to be gone. However, the code below works as expected:
var myController = null;
public static AddView()
{
this.myController = new MyController();
window.AddSubView(myController.View);
}
Upvotes: 2
Views: 635
Reputation: 292
Monotouch is built on top of iOS framework which at its core is using reference counting as its memory management. Even after adding garbage collection it does not affect the underlying framework. In the case of UIView it is true that it holds a reference to its UIViewController and the reason why your controller gets garbage collected is because all delegates in iOS are assigned and NOT retained. Apples memory management guide explains this in greater detail under "Weak References to Objects": http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html%23//apple_ref/doc/uid/20000043-BEHDEDDB
Hope this helps.
Upvotes: 3