获取模型从点击NSCollectionVIew项目 [英] Getting model from click on NSCollectionVIew item

查看:738
本文介绍了获取模型从点击NSCollectionVIew项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试一个基本的应用程序与选项卡式界面,并已经几年了我做过任何可可开发,所以我生锈...我的应用程序将使用选项卡,并显示默认当您加载标签时,您可以点击其中一个项目来交换该项目的视图(当您打开一个新标签页,并显示您最喜欢的网站,您可以点击打开) 。

I'm trying to put up a basic app with a tabbed interface, and it's been a few years since I've done any cocoa development so I'm rusty... My app will use tabs, and display a default view of items when you load a tab, allowing you to click on one of the items to swap in a view for that item (think chrome, when you open a new tab and it displays your favorite sites which you can click on to open).

目前我已经实现了一个基本的单一窗口UI,几乎完全在界面生成器。一个带有NSCollectionView的窗口,在窗口控制器中使用与ArrayController和NSMutableArray的集合项的绑定。工作太棒了。

Currently I have implemented a basic single window UI for this almost entirely in interface builder. A window with an NSCollectionView, using bindings with an ArrayController and NSMutableArray of collection items in my window controller. Works great.

有两件事情,但是,我不知道,它让我想知道我是否更好地在这里做编程,而不是搞乱界面生成器。具体来说,我不知道该怎么办:

There are two things, however, I'm not sure about and it makes me wonder if I'm better off doing things programmatically here instead of messing with Interface Builder. Specifically the two things I'm not sure how to do:


  1. 获取NSCollectionView上的点击事件并将其映射到点击的模型

  2. 在新标签中加载此收藏视图的新实例(用户可以打开多个标签,所有标签都应显示项目网格)

对于#1我真的看不到一个伟大的方法来做到这一点。我可以捕获在窗口控制器中的mouseDown,但我不知道如何映射回到模型。我还为我的集合视图项目创建了一个NSView子类,但是我无法找出一种方法将该视图映射到模型...

For #1 I don't really see a great way to do this. I can capture mouseDown in the window controller, but I have no idea how to map that back to the model. I also created a NSView subclass for my collection view item, however I can't figure out a way to map that view to the model either...

对于#2我不认为有一个伟大的方法来做到这一点,除了以编程方式创建视图。我唯一的另一个想法是把视图放在另一个.xib中,并在需要时使用它来创建新的视图。这可能会工作正常,如果我能弄清楚#1 ...

For #2 I don't think there's a great way to do this, other than creating the views programmatically. The only other thought I had was to put the view in another .xib and use that to create new views when needed. Which would probably work fine if I could figure out #1...

所以,我最好只是创建集合视图以编程方式,或有一些我可以在这里?

So, am I better off just creating the collection view programmatically or is there something I can do here?

推荐答案

NSCollectionViewItem NSViewController ,您可以在其上设置/获取resentObject 。我总是使用它来访问底层对象。

NSCollectionViewItem is a subclass of NSViewController, on which you can set/get the representedObject. I always use that for access to the underlying object.

⑵你绝对要拆分你的XIB。每次我做完,我发现这使我的工作更容易。

⑵ You absolutely want to split up your XIBs. Every time I’ve done it, I find it’s made my job easier.

这篇关于获取模型从点击NSCollectionVIew项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆