从 NSMenuItem 获取 NSPopUpButton

发布于 2024-08-19 10:54:37 字数 285 浏览 7 评论 0原文

我的应用程序使用 -validateMenuItem: 方法来验证菜单项。 但我需要根据弹出窗口中的内容验证不同的菜单项。

我希望有一种方法来获取弹出窗口的标签,但在查看文档后我似乎找不到方法......有什么想法吗?

编辑: 我认为这需要更多上下文...我的模型对象是 JDBCSyncer (将一个数据库与另一个数据库同步),我的窗口是一个设置窗口,我需要根据菜单项的标题是否在字符串数组中来验证菜单项它代表表中的各个字段。这个想法是您从弹出窗口中选择字段。

My application uses the -validateMenuItem: method for validating menu items.
But I need to validate different menu items depending on what popup's there in.

I was hoping for a way to get the tag of the popup, but after looking through the docs I can't seem to find a way... any ideas?

Edit:
I thought this needed some more context... my model object is a JDBCSyncer (syncs one database with another), my window is a settings one, and I need to validate my menu items based on wether their title is in an array of strings which represents the various fields within a table. The idea is that you select the field from a popup.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-08-26 10:54:37

我的脑海中没有一个很好的答案,但是沿着这些思路怎么样:

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    NSMenu *menu = [menuItem menu];
    if (menu == [popUpButton1 menu]) {
        return YES;
    }
    else if (menu == [popUpButton2 menu]) {
        return NO;
    }
    else (menu == [popUpButton3 menu]) {
        return YES;
    }
    else {
        return NO;
    }
}

I don't have a great answer off the top of my head, but how about something along these lines:

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    NSMenu *menu = [menuItem menu];
    if (menu == [popUpButton1 menu]) {
        return YES;
    }
    else if (menu == [popUpButton2 menu]) {
        return NO;
    }
    else (menu == [popUpButton3 menu]) {
        return YES;
    }
    else {
        return NO;
    }
}
野の 2024-08-26 10:54:37

如果这些弹出按钮中的菜单项非常不相关,以至于您需要区分一个弹出按钮与另一个,则也许您应该为它们创建单独的控制器对象。每个控制器将是其弹出按钮的菜单项的目标(因此也是验证器),并且那个/那些弹出按钮。

如果这对于相关项目有意义的话,部分或全部控制器还可以提供表视图或集合视图。

这还可以让您标记菜单项以便于识别,而不必担心标签冲突(两个或多个不相关的 UI 元素中使用相同的标签),因为每个控制器只会看到它知道的标签。类似地,如果控制器使用表示对象(最有可能的是它动态填充其弹出按钮),则它不必担心看到它无法识别的表示对象。

If the menu items in these pop-up buttons are so unrelated that you need to distinguish one pop-up button from the other, perhaps you should create separate controller objects for them. Each controller would be the target (and, thus, the validator) of its pop-up button(s)' menu items, and only that/those pop-up button(s).

Some or all of these controllers can also feed table views or collection views, if that makes sense for the items in question.

This also lets you tag the menu items for easy identification, without worrying about tag collisions (same tag used in two or more unrelated UI elements), since each controller will only see the tags it knows about. Similarly, if a controller uses represented objects (most probable if it dynamically populates its pop-up button(s)), it doesn't have to worry about seeing represented objects it doesn't recognize.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文