Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement multiple delegates in Monotouch?

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# ?

like image 949
driis Avatar asked Apr 28 '13 18:04

driis


People also ask

How to call multiple methods using a delegate?

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.

How to assign multiple objects to one delegate?

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.

How to combine delegates in multicasting?

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.

What is MonoTouch dialog?

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.


1 Answers

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.

like image 75
jonathanpeppers Avatar answered Sep 21 '22 00:09

jonathanpeppers