If I create a view, such as a toolbar, in ViewDidLoad of my ViewController dynamically and add it to the page, storing the toolbar as a private variable - do I need to do anything special to have it disposed of properly (will it prevent my ViewController from getting cleaned up if the user clicks back etc.)? I would think in OnDispose I can check for null, call Dispose on the toolbar and null it out - but is that too late (will it keep my parent ViewController in memory)?
Also, I understand how you should subscribe and unsubscribe from event handlers in ViewWillAppear and ViewWillDisappear, if I create a UIView subclass (a custom control) in ViewDidLoad and add it as a subview of my ViewController, and it in turn dynamically instantiates child views that are hooked up via subscriptions (button.Click += handler etc.) do these also need to have their event handlers added and removed when the custom UIView subclass appears and disappears for cleanup to happen properly?
Thanks.
Posts
Use lambda for click event instead of delegates for example :
button.Click += (sender, e) => { ... }
Also work with using statements for disposable objects.
Check documentation here
The lambdas don't allow your ViewController to be disposed from what I've read on StackOverflow - it being better to explicitly subscribe and unsubscribe in ViewWillAppear and ViewWillDisappear? But the documentation doesn't talk about ViewController's specifically: https://developer.xamarin.com/guides/cross-platform/deployment,_testing,_and_metrics/memory_perf_best_practices/#events
I am using using statements when I use a variable that is only in method scope, but I'm referring to the case where I instantiate a UIView, such as a toolbar, in ViewDidLoad and add it to my subviews, but it is also stored as a private variable since it is used elsewhere in the class.
Here is a post that talked a lot about events for Xamarin iOS, I did not notice any problem because of lambda or delegate or even Toolbar. Also here, another topic about your question that may help more.
Here's an example. Assume this ViewController is segued to from another ViewController within a NavigationController. If I don't make sure the event is gone in ViewWillDisappear, when clicking back on the device the ViewController is not disposed. If I do, I can see Dispose getting called.