1

我正在尝试NSTouchBar用 3 个级别做一个简单的NSPopoverTouchBarItem,所以基本上是这样的:

触控栏 我有NSTouchbar3NSButton和 1的NSPopoverTouchBarItem主要打开第二个NSTouchbar

第二个NSTouchbar用 2NSButton和 1NSPopoverTouchBarItem打开第三个NSTouchbar

问题是当我尝试打开第 3 个时NSTouchbar,似乎第 2 个NSTouchbar被关闭,有时不打开第 3 个。

同样,当打开第三个时,当我们关闭时,我们转到第一个NSTouchbar,而不是第二个NSTouchbar

这是代码,应该很简单,并且应该可以工作(我正在使用 Xcode TouchBar Simulator

#import "Window.h"

static NSTouchBarCustomizationIdentifier TouchBarCustomizationIdentifier    = @"TouchBarCustomizationIdentifier";

static NSTouchBarItemIdentifier NSTouchBarItemIdentifier1                   = @"NSTouchBarItemIdentifier1";
static NSTouchBarItemIdentifier NSTouchBarItemIdentifier2                   = @"NSTouchBarItemIdentifier2";
static NSTouchBarItemIdentifier NSTouchBarItemIdentifier3                   = @"NSTouchBarItemIdentifier3";
static NSTouchBarItemIdentifier NSTouchBarItemIdentifier4                   = @"NSTouchBarItemIdentifier4";

static NSTouchBarItemIdentifier NSTouchBarItemIdentifier4_1                 = @"NSTouchBarItemIdentifier4_1";
static NSTouchBarItemIdentifier NSTouchBarItemIdentifier4_2                 = @"NSTouchBarItemIdentifier4_2";
static NSTouchBarItemIdentifier NSTouchBarItemIdentifier4_3                 = @"NSTouchBarItemIdentifier4_3";

static NSTouchBarItemIdentifier NSTouchBarItemIdentifier4_3_1               = @"NSTouchBarItemIdentifier4_3_1";
static NSTouchBarItemIdentifier NSTouchBarItemIdentifier4_3_2               = @"NSTouchBarItemIdentifier4_3_2";

@implementation Window

- (NSTouchBar*) makeTouchBar {

    _touchBar1 = [[NSTouchBar alloc] init];
    [_touchBar1 setDelegate:self];
    [_touchBar1 setCustomizationIdentifier:TouchBarCustomizationIdentifier];

    [_touchBar1 setDefaultItemIdentifiers:@[
                                            NSTouchBarItemIdentifier1,
                                            NSTouchBarItemIdentifier2,
                                            NSTouchBarItemIdentifier3,
                                            NSTouchBarItemIdentifier4,
                                            ]
     ];
    [_touchBar1 setCustomizationRequiredItemIdentifiers:@[
                                                          NSTouchBarItemIdentifier1,
                                                          NSTouchBarItemIdentifier2,
                                                          NSTouchBarItemIdentifier3,
                                                          NSTouchBarItemIdentifier4,
                                                          ]
     ];

    return _touchBar1;
}

- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {

    if ([identifier isEqual:NSTouchBarItemIdentifier1]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH 1" target:self action:nil]];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier2]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];

        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH1" target:self action:nil]];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier3]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];

        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH1" target:self action:nil]];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier4]) {

        NSPopoverTouchBarItem *customTouchBarItem = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
        [customTouchBarItem setCollapsedRepresentationLabel:@"OPEN TOUCH 2"];

        _touchBar2 = [[NSTouchBar alloc] init];
        [_touchBar2 setDelegate:self];
        [_touchBar2 setCustomizationIdentifier:TouchBarCustomizationIdentifier];

        [_touchBar2 setDefaultItemIdentifiers:@[
                                                NSTouchBarItemIdentifier4_1,
                                                NSTouchBarItemIdentifier4_2,
                                                NSTouchBarItemIdentifier4_3,
                                                ]
         ];
        [_touchBar2 setCustomizationRequiredItemIdentifiers:@[
                                                              NSTouchBarItemIdentifier4_1,
                                                              NSTouchBarItemIdentifier4_2,
                                                              NSTouchBarItemIdentifier4_3,
                                                              ]
         ];

        [customTouchBarItem setPopoverTouchBar:_touchBar2];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier4_1]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];

        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH 2" target:self action:nil]];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier4_2]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];

        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH 2" target:self action:nil]];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier4_3]) {

        NSPopoverTouchBarItem *customTouchBarItem = [[NSPopoverTouchBarItem alloc] initWithIdentifier:identifier];
        [customTouchBarItem setCollapsedRepresentationLabel:@"OPEN TOUCH 3"];

        _touchBar3 = [[NSTouchBar alloc] init];
        [_touchBar3 setDelegate:self];
        [_touchBar3 setCustomizationIdentifier:TouchBarCustomizationIdentifier];

        [_touchBar3 setDefaultItemIdentifiers:@[
                                                NSTouchBarItemIdentifier4_3_1,
                                                NSTouchBarItemIdentifier4_3_2,
                                                ]
         ];
        [_touchBar3 setCustomizationRequiredItemIdentifiers:@[
                                                              NSTouchBarItemIdentifier4_3_1,
                                                              NSTouchBarItemIdentifier4_3_2,
                                                              ]
         ];

        [customTouchBarItem setPopoverTouchBar:_touchBar3];


        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier4_3_1]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];

        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH 3" target:self action:nil]];

        return customTouchBarItem;

    } else if ([identifier isEqual:NSTouchBarItemIdentifier4_3_2]) {

        NSCustomTouchBarItem *customTouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];

        [customTouchBarItem setView:[NSButton buttonWithTitle:@"IS TOUCH 3" target:self action:nil]];

        return customTouchBarItem;

    }

    return nil;
}

@end
4

1 回答 1

0

苹果说popovers不能有popovers,所以你只能有一个bar和一个popover,而不是popover里面的popover。

这是他们在论坛上发布的回答类似问题的内容:

在此处输入图像描述

https://forums.developer.apple.com/thread/78730

于 2017-07-08T12:38:54.557 回答