Question

I need to add a delegate to my MainWindowController - which has all the outlets and actions and drives the program. In ObjectiveC, the following would work

NSWindowController < NSPopoverDelegate >

How would I do this in MonoMac? Again, to put it a different way, I would like my NSWindowController to be the delegate.

Was it helpful?

Solution

The standard way to do this in MonoMac is to nest a private class inside your MainWindowController that derives from NSPopoverDelegate. Because your delegate has to derive from the base delegate class, you can't really make your MainWindowController itself the delegate.

e.g.

public partial class MainWindowController : MonoMac.AppKit.NSWindowController
{
    public override void AwakeFromNib()
    {
        myPopover.Delegate = new MyPopoverDelegate();

        base.AwakeFromNib();
    }

    private class MyPopoverDelegate : NSPopoverDelegate
    {
        public override bool ShouldClose(NSPopover popover)
        {
            // etc...
        }
    }

If you really want your MainWindowController to function as the delegate, you can use the WeakDelegate property (which takes an object) and manually export the delegate methods. This is not the preferred pattern:

    public override void AwakeFromNib()
    {
        myPopover.WeakDelegate = this;

        base.AwakeFromNib();
    }

    [Export("popoverShouldClose:")]
    private bool PopoverShouldClose(NSPopover popover)
    {
        // etc...
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top