如何在 Cocoa 中实现基于缩略图的用户选择视图?
我有一个我认为非常简单的需求,但我不知道如何去实现它。
在我的界面中的各个点上,用户被要求做出一些相当直观的选择(例如选择页面布局或颜色选项)。这些选择是从一组预定义的选项中做出的。通常您会使用 NSPopUpButton,或者 NSTableView,但我希望用户看到每个选择选项的缩略图。这些缩略图也应该有一个合适的尺寸——至少 100x100 像素。每个缩略图还应附有文本标签。一次只允许选择一项。
Apple 在 iWeb 中实现了类似的功能:
iweb http://img187.imageshack.us/ img187/5744/iweb.jpg
当然,您已经习惯了看到这样的小图标:
我认为 NSCollectionView 是一个开始的地方,但我只见过它在更复杂的场景中使用(例如:显示目录的内容,支持拖放等)因此,大多数我发现代码相当复杂。
是否有更简单的解决方案来使用静态的预定义数据填充此视图并仅允许进行简单的选择?换句话说,我正在寻找弹出按钮的功能,但需要不同的用户界面。
更新:解决方案
我按照建议使用了 IKImageBrowser。 本教程非常有帮助。在IB中:我只是在界面中制作了一个IKImageBrowser,将其包装在滚动视图中,并将其作为浏览器对象的数据源。在代码中:按照教程中的说明在我的控制器中实现数据源协议,将 allowedMultipleSelection 和 allowedEmptySelection 设置为 NO,完全按照教程中的说明使用 URL 图像表示创建自定义图像对象。
然后,为了获得选择,我编写了以下方法,以便在用户单击“确定”按钮时触发:
NSIndexSet *retval = [imageBrowser selectionIndexes];
NSInteger i = [retval firstIndex]; //the index of the selection. Corresponds to index in my array data source
NSString* path = [[styleViewData objectAtIndex:i] imageRepresentation]; //path to selected image, if you need it
该教程非常有帮助 - 只需忽略有关添加图像和缩放滑块等内容。
I have what I thought was a pretty simple need but I'm not sure how to go about implementing it.
At various points in my interface, the user is asked to make a selection of something rather visual (like choosing a layout for a page or a color option.) These selections are made from a pre-defined set of options. Normally you'd use an NSPopUpButton, or maybe an NSTableView, but I would like the user to see thumbnails of each selection option. These thumbnails ought to be a decent size, too--at least 100x100 pixels. Each thumbnail should also be accompanied by a text label. Only one selection is allowed at once.
Apple implements something rather like this in iWeb:
iweb http://img187.imageshack.us/img187/5744/iweb.jpg
Of course, you're used to seeing something like this with small icons:
I'm thinking that an NSCollectionView is the place to start, but I've only ever seen this used in much more complicated scenarios (eg: displaying the contents of a directory, supporting drag and drop, etc.) Therefore, most of the code I've found is rather complicated.
Is there a simpler solution to populate this view with static, pre-defined data and just allow for a simple selection? In other words, I'm looking for the functionality of a pop-up button but a different user interface.
Update: solution
I used IKImageBrowser, as suggested. This tutorial was very helpful. In IB: I just made an IKImageBrowser in my interface, wrapped it in a scroll view, and made it the data source of the browser object. In code: implemented the data source protocol in my controller like the tutorial explains, set allowsMultipleSelection and allowsEmptySelection to NO, created a custom image object using the URL image representation exactly as explained in the tutorial.
Then, to get the selection, I wrote the following method to be triggered when the user clicks the "OK" button:
NSIndexSet *retval = [imageBrowser selectionIndexes];
NSInteger i = [retval firstIndex]; //the index of the selection. Corresponds to index in my array data source
NSString* path = [[styleViewData objectAtIndex:i] imageRepresentation]; //path to selected image, if you need it
The tutorial is very helpful--just ignore the stuff about adding images and zoom sliders and such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要 IKImageBrowserView。
You want IKImageBrowserView.