문제

I have application based on NSDocument (NSPersistentDocument), in application I can create (as usually) more than one document.

Main document window (based on NSPersistentDocument) has, added IB, toolbar. In code I add to toolbar item (NSToolbarItems) using methods insertItemWithItemIdentifier and - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag.

Code samples:

[_toolbar insertItemWithItemIdentifier:@"addTape" atIndex:2];

and

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
    NSToolbarItem *item =nil;

    if ([itemIdentifier isEqual:@"addTape"]) {
        item = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
        item.label = NSLocalizedString(@"Add Tape",@"Add Tape");
        item.paletteLabel = NSLocalizedString(@"Add Tape",@"Add Tape");
        item.toolTip = NSLocalizedString(@"Adds new tape",@"Adds new tape");
        item.image = [NSImage imageNamed:@"NSAddTemplate"];
        item.target = self;
        item.action = @selector(addTape:);
        item.tag = 101;         
    }
}

Everything is correct until I have opened only one document. When I open second document (or create new document) on first document window toolbar items are doubled (after opening third document, items are tripled on first window and doubled on second, and so on).

Edit: I noticed, that itemForItemIdentifier is called on each window, everytime I try to add toolbar item. In example: if I have two windows (two opened document) and on one I try to add one button itemForItemIdentifier is called two times.
It is strange to me, because every document has own toolbar with delegate set only to this document.

I don't have any idea what I have done wrong. Maybe someone will point me where I made a mistake.

도움이 되었습니까?

해결책 2

I found solution: I cannot use tooolbar created from nib, because each created this method toolbar has this same identifier. When I created toolbars in code, using different identifiers, the problem has gone.

다른 팁

You don't typically insert toolbar items yourself; implement the delegate methods toolbarAllowedItemIdentifiers: and toolbarDefaultItemIdentifiers: and the toolbar will be initialized according to those lists.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top