问题
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