首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多级NSTouchBar的NSPopoverTouchBarItem

具有多级NSTouchBar的NSPopoverTouchBarItem
EN

Stack Overflow用户
提问于 2016-12-05 15:46:21
回答 1查看 589关注 0票数 1

我试图用3个级别的NSTouchBar制作一个简单的NSPopoverTouchBarItem,基本上如下所示:

我有主NSTouchbar,有3 NSButton和1 NSPopoverTouchBarItem,它打开了第二个NSTouchbar

具有2 NSButton和1 NSPopoverTouchBarItem的第二个NSTouchbar,它打开第三个NSTouchbar

问题是,当我尝试打开第三NSTouchbar,似乎第二NSTouchbar被解雇,有时不打开第三。

另外,当打开第三个NSTouchbar时,当我们关闭时,我们进入第一个NSTouchbar,而不是第二个NSTouchbar

下面是代码,应该很简单,并且应该可以工作(我使用的是Xcode TouchBar模拟器)

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

发布于 2017-07-08 12:38:54

苹果说,爆米花不能有爆米花,所以你只能有一个酒吧和一个爆米花,而不是一个爆米花里面的爆米花。

这是他们在论坛上发表的回答类似问题的文章:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40978019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档