0

I'm porting the Official Apple Multipeer WiTap sample to Xamarin and C#, however I'm not sure how to port this method within TapViewController.

It contains all the things I'm not sure how to port... namely

  1. A delegate that is expressed only in code, and has no strong delegate in the Xamarin framework. (this is probably easy, just a concept I'm missing)
  2. What the heck is the id here, and how do I declare/use it?
  3. How do I declare self.delegate;

code:

- (IBAction)closeButtonAction:(id)sender
{
    id <TapViewControllerDelegate>  strongDelegate;

    #pragma unused(sender)
    strongDelegate = self.delegate;
    if ([strongDelegate respondsToSelector:@selector(tapViewControllerDidClose:)]) {
        [strongDelegate tapViewControllerDidClose:self];
    }
}

Here is a link to my code, where the port is in progress

makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

1

Are you attempting to port Objective-C code to C# without knowing the former?

  • id is Obj-C's "any object", while id<protocolName> is any object which implements the specified protocol. An Obj-C "delegate" is just an object which implements a given protocol, it is the way it is used that makes it a delegate. Objc-C protocols and C# interfaces are matching concepts. So strongDelegate is a variable whose type is a C# interface (which you've presumably translated from the Obj-C protocol).
  • Declaring self.delegate: it's an Objc-C property reference with the same (or compatible) type as strongDelegate. C# has properties.
  • The if test in Obj-C is determine whether the referenced object implements the specified method and if so invokes it. Obj-C protocols allow optional methods, i.e. methods objects implementing the protocol need not implement. C# interfaces have no direct equivalent. In your translation of the Obj-C protocol to a C# interface you either made all optional methods non-optional, or you did something else. Translate the if to match whatever you did.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86