Adding a copy of an NSTabViewItem from an NSTabView into the same NSTabView

有些话、适合烂在心里 提交于 2019-12-10 22:07:33

问题


I'm creating a simple web browser and would like to implement tabs. For this purpose, I'm using an NSTabView. I basically want each tab to have a WebView that will show the website loaded. I'm starting with only one tab and want to add an exact copy of the first one when I create a new tab. I tried something like tabView.addTabViewItem(tabView.tabViewItemAtIndex(0).copy() as NSTableViewItem) but I'm getting an unrecognised selector sent to instance error. I've check the documentation for both NSTableView and NSTableViewItem but can't figure out how to that.

EDIT My whole error looks like this:

015-03-14 17:15:57.884 Browser[1955:56547] -[NSTabViewItem copyWithZone:]: unrecognized selector sent to instance 0x600000100b40
2015-03-14 17:15:57.884 Browser[1955:56547] -[NSTabViewItem copyWithZone:]: unrecognized selector sent to instance 0x600000100b40

回答1:


Thanks to the answer on Copy NSView in cocoa and @MattyAyOh, I solved my problem by doing:

var data = NSKeyedArchiver.archivedDataWithRootObject(view)
var newView = NSKeyedUnarchiver.unarchiveObjectWithData(data) as NSView
newTab.view = newView
tabView.addTabViewItem(newTab)



回答2:


So you are getting that error because you are calling copy() on an object that doesn't have -copyWithZone implemented. (Because NSTabViewItem subclasses NSView which doesn't conform to the NSCopying protocol)

What you can do to get around that is to initialize a new NSTabViewItem, then get the properties from the first item (tabViewItemAtIndex(0)), and then set them to your new NSTabViewItem

from the documentation

you can call setLabel, setIdentifier, setColor, setView, and setTooltip

Once you've initialized and set your new NSTabViewItem, you can then add it to your NSTabView

NSTabViewItem *tempTabViewItem = [NSTabViewItem new];
//set your properties on tempTabViewItem here
tabView.addTabViewItem(tempTabViewItem);


来源:https://stackoverflow.com/questions/29050513/adding-a-copy-of-an-nstabviewitem-from-an-nstabview-into-the-same-nstabview

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