Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Usage of NSTabView.DidSelect event

Im using NSTabViewController to create a preferences window. In order to execute some code when the active tab changes, I would like to use NSTabView's DidChange event (as also suggested in Xamarin Forums discussion/comment/118342/#Comment_118342 - unfortunately I can't post links yet).

This is what I have:

var tabViewController = new NSTabViewController();
tabViewController.TabView.DidSelect += (sender, e) => // exception thrown here
      // do stuff here

Unfortunately, an InvalidOperationException is thrown when attaching the event handler:

Event registration is overwriting existing delegate. Either just use events or your own delegate: AppKit.NSTabViewController AppKit.NSTabView+_NSTabViewDelegate

I am actually only using events and not using my own delegate (contrary what the exception message is saying). Or do I misinterpret this?

Anyway, I checked the (generated) source code for NSTabView (/Library/Frameworks/Xamarin.Mac.framework/Versions/ This is what I understand (and I might be wrong...): In order to use the DidSelect event, the Delegate property should be of (internal) type _NSTabViewDelegate. I also saw that if I set Delegate to null before using DidSelect, an instance of _NSTabViewDelegate is created and everything should be okay, so I came up with this:

var tabViewController = new NSTabViewController();
tabViewController.TabView = null; // silently crashing here
tabViewController.TabView.DidSelect += (sender, e) =>
      // do stuff here

Unfortunately, the app silently crashes without any exception when assigning null.

Can someone explain how DidSelect event should/can actually be used?
Thanks in advance!



  • YuriKuznetsovYuriKuznetsov USMember ✭✭✭

    you can create your own delegate something like that

    public class MyDelegate : NSTabViewDelegate {
        public event Action DidSelected;  
        public override void DidSelect(NSTabView tabView, NSTabViewItem item)

    and you need to initialize TabView

    var dlg = new MyDelegate();
    dlg.DidSelected += () => {
            //you code here        
    var tabViewController = new NSTabViewController();
    tabViewController.TabView.Delegate = dlg; 
  • johannes_schmittjohannes_schmitt Member ✭✭

    Hi @YuriKuznetsov , thanks for sharing your the idea.
    Unfortunately, the app is crashing when I assign the delegate:
    tabViewController.TabView.Delegate = dlg;

    The unhandled exception is an NSInternalInconsistencyException:

    A TabView managed by a TabViewController cannot have its delegate modified

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai


    Did you try just using a NSTabView instead of the NSTabViewController?

    If you want to use the NSTabViewController, I would recommend subclassing it and then overriding the DidSelect method.

    A simple sample is attached.

Sign In or Register to comment.