Override delegate in supplied Obj-C library in C#

pauldpauld USMember ✭✭
edited January 2013 in Xamarin.iOS


I'm working on integrating with a third-party hardware accessory. The iOS library that I'm binding to (successfully) also has some UI elements (UIAlertViews) that it displays, and I have requested that I be able to override these messages. The developers altered the library so it calls a number of class methods on a Obj-C class called AlertMessages, which is a UIAlertViewDelegate. This class appears to be linked into the supplied hardware library I'm binding to, but the example Xcode project also has an AlertMessages.m class that re-implements the class. The Objective-C runtime seems to know that it should use this external AlertMessages.m for the class, rather than the one embedded in the library - if I remove the .m file, the messages still work as they come from the library, and if I change the message text in AlertMessages.m then I see the changed text. All fine.

So, I'm now trying to implement my own AlertMessages class in C#, but I can't get the library or runtime to see my class, it just defaults back to the one in the .a I've bound to - note that I've bound to other classes in the library, but not specifically the AlertMessage class.

Here's what I have so far:

using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;

namespace Cosmos.iOS
    public class AlertMessages : UIAlertViewDelegate
        static Selector selInit       = new Selector("init");

        public AlertMessages ()
            : base(NSObjectFlag.Empty)
            Handle = Messaging.IntPtr_objc_msgSend(this.Handle,

        public AlertMessages (IntPtr handle)
            : base(handle)

        public static void alertMessagesplayAlarm()
            Console.WriteLine("alertmessage 1");

                /// ... other class methods

At the moment, none of the methods in my class are getting called, but I'm not sure why. I can't see anything in their sample code that specifies which delegate to use, and I don't think that would explain the ability to override it from the .m file.




  • ChrisHardyChrisHardy GBXamarin Team Xamurai

    How come you are writing the bindings manually yourself? Why do you use the binding library tools available to do the hard work for you: http://docs.xamarin.com/ios/Guides/Advanced_Topics/Binding_Objective-C_Libraries ?

  • pauldpauld USMember ✭✭

    Yes, I did use the binding tools to bind to the Obj-C library, I followed that guide you linked to. So I can call into the Obj-C library from C#.

    Where I'm stuck is creating a C# class that Obj-C can call into - the opposite direction. The information on doing that isn't as clear (to me). I ended up basing my AlertMessages (UIAlertViewDelegate) on the last section on this page: http://docs.xamarin.com/ios/Guides/Advanced_Topics/Binding_Objective-C_Libraries/binding_details

    I started off just using the RegisterAttribute and ExportAttribute but in desperation added the selector stuff, but I think that's not helping.

    Thinking about it some more, I think the code in the library I'm binding to must be working out if an AlertMessages class exists and uses it if present - I need to work out how it's doing that and get my class to appear in the same way.

    This is the class I need to implement in C#:

    @interface AlertMessages : NSObject <UIAlertViewDelegate>{
    // ...more class methods
Sign In or Register to comment.