create a subclass of NSView to enable setTag()

蹲街弑〆低调 提交于 2020-01-17 06:09:41

问题


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


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/44377881/create-a-subclass-of-nsview-to-enable-settag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!