简体   繁体   中英

create a subclass of NSView to enable setTag()

I am using xamarin to develop a Mac app, and somewhere in my program I want to set the tag of a NSView. However, the tag property is readonly for NSView, so I'm searching for a way to create a subclass where tag is writable. Is there any suggestion about how I should write the subclass? thanks

public class MyNSView : NSView
{
    public nint _tag;

    public new nint Tag
    {
        get
        {
            return _tag;
        }
        set
        {
            _tag = value;
        }
    }

Be aware that this is not longer the NSView Tag.

Using your custom NSView Tag

var view = new MyNSView ();

view.Tag = 100;

I found a workaround to get viewWithTag working:

@IBInspectable
override var tag: Int {
    get { return super.tag  }
    set { super.tag = newValue } 
}

In IB the NSView's Tag is still greyed out, but there is also a subclass's Tag available for setting a value.

Still, to my best effort, I can't find a logical reason for Tag to be greyed out in IB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM