In Objective-C, delegates such as UITableViewControllerDelegate are protocols, so I can implement multiple in one class.
In Monotouch, all the iOS delegates are exposed as C# classes. This makes it impossible to implement two delegate on a single controller class, since C# (rightly, IMO) only allows single inheritance.
Does the delegate declarations exist as C# interfaces somewhere in the Monotouch framework ? (This would be the closest we could come to the protocol from Objective-C)
What are my options if I need to implement multiple delegates on a class in Monotouch / C# ?
If you want to call multiple methods using a delegate then all the method signatures should be the same. Let us see an example for understanding the Multicast Delegate in C#. Please have a look at the following example which is without using delegates.
A useful property of delegate objects is that multiple objects can be assigned to one delegate instance by using the + operator. The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined.
In Multicasting, Delegates can be combined and when you call a delegate, a whole list of methods is called. All methods are called in FIFO (First in First Out) order.
MonoTouch.Dialog, referred to as MT.D for short, is a rapid UI development toolkit that allows developers to build out application screens and navigation using information, rather than the tedium of creating view controllers, tables, etc. As such, it provides a significant simplification of UI development and code reduction.
You can use WeakDelegate
to do this, but you have to have all the Export
decorations correct. Documentation on weak delegates near the bottom here. Here is an example of UITableViewSource
with weak delegates.
I'm not sure exactly why Xamarin had to use classes instead of interfaces for Obj-C protocols, but I'm guessing it is a limitation they had to workaround.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With