如何使用 NSBrowser 中所选单元格的路径更新 NSPathControl

发布于 2024-07-10 05:26:18 字数 288 浏览 3 评论 0原文

我需要使用 NSBrowser 中当前选定的路径来更新 NSPathControl,但是当 NSBrowser 的路径发生更改时,我无法找到获取通知的方法。 执行此操作的理想方法是观察 NSBrowser 中的路径 key 路径,但这使得 KVO 只能观察返回 void 消息且没有更新的 set 方法(setPath返回 bool 成功值)。

我还尝试观察 selectedCell 键路径,但当选择更改时我没有收到通知。

我缺少其他一些真正明显的方法来做到这一点吗?

I need to keep an NSPathControl updated with the currently selected path in an NSBrowser, but I'm having trouble figuring out a way of getting notifications when the path has changed from the NSBrowser. The ideal way to do this would just to be to observe the path key path in the NSBrowser, but that gives a KVO can only observe set<key> methods which return void message and no updates (setPath returns a bool success value).

I also tried observing the selectedCell key path, but I'm not getting notifications when the selection there is changed.

Is there some other really obvious way to do this that I'm missing?

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

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

发布评论

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

评论(3

黑寡妇 2024-07-17 05:26:18

由 Rob Keniger 在 Cocoa Dev 提供:

你看过SimpleBrowser吗?
/Developer/Examples 中的示例? 它
显示如何获取当前选择
当用户更改时,
基本上只需设置
NSBrowser 的操作。

这确实是这样做的方法。 只需在控制器中实现类似 - (void)browserClicked: 的方法,并将其映射到界面生成器中的 NSBrowseraction每次在该方法内选择更改时都希望发生,例如

- (void)browserClicked:(id)browser {
    self.pathToSelectedCell = [browser path]; // NSPathControl is bound to pathToSelectedCell
}

Courtesy of Rob Keniger over at Cocoa Dev:

Have you looked at the SimpleBrowser
example in /Developer/Examples? It
shows how to get the current selection
when it is changed by the user,
basically by just setting up the
action of the NSBrowser.

That is indeed the way to do it. Just implement a method like - (void)browserClicked: in your controller and map it to the NSBrowser's action in interface builder with whatever you want to happen each time the selection changes inside that method, e.g.

- (void)browserClicked:(id)browser {
    self.pathToSelectedCell = [browser path]; // NSPathControl is bound to pathToSelectedCell
}
千紇 2024-07-17 05:26:18

我刚刚检查了 IB,看起来 NSBrowser 有一个选择索引路径绑定(NSIndexPath 对象的数组),您可以使用 KVO 进行监视。 这很奇怪,但我在文档中没有看到任何提及它,所以你可能需要做一些研究来看看这是你应该或不应该使用的东西,即使它看起来有效。 如果是这样,在您的 KVO 观察方法中,您将找到浏览器的当前路径,并将其转换为路径控件可以使用的 NSURL。

如果这不起作用,还有委托方法 - (BOOL)browser:(NSBrowser *)sender selectRow:(NSInteger)row inColumn:(NSInteger)column- (BOOL)browser :(NSBrowser *)发送者 selectCellWithString:(NSString *)标题 inColumn:(NSInteger)列

I just checked in IB, and it looks like NSBrowser has a selection index paths binding (an array of NSIndexPath objects) that you could possibly monitor with KVO. It's strange but I don't see any mention of it in the docs, so you might need to do a little research to see if that's something you should or shouldn't use, even if it seems to work. If it does, in your KVO observation method you would find the browser's current path, and convert that to an NSURL the path control can use.

If that doesn't work there's also the delegate methods - (BOOL)browser:(NSBrowser *)sender selectRow:(NSInteger)row inColumn:(NSInteger)column and - (BOOL)browser:(NSBrowser *)sender selectCellWithString:(NSString *)title inColumn:(NSInteger)column.

能怎样 2024-07-17 05:26:18

从 10.6 开始,可以通过使用委托回调来找出选择了哪些项目,如下所示:

- (NSIndexSet *)browser:(NSBrowser *)browser selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes inColumn:(NSInteger)column
{
    NSLog(@"New first item of the new selection is at index %@", [proposedSelectionIndexes firstIndex]);
    // Do something with the selected index or indicies
    return proposedSelectionIndexes; // Allow the selection to occur by not changing this
}

As of 10.6, one can find out which items are selected, by using the delegate callback as follows:

- (NSIndexSet *)browser:(NSBrowser *)browser selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes inColumn:(NSInteger)column
{
    NSLog(@"New first item of the new selection is at index %@", [proposedSelectionIndexes firstIndex]);
    // Do something with the selected index or indicies
    return proposedSelectionIndexes; // Allow the selection to occur by not changing this
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文