如何处理来自NSCollectionView的按钮点击 [英] How to handle a button click from NSCollectionView

查看:462
本文介绍了如何处理来自NSCollectionView的按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSCollectionView (OS X,而不是iOS)绑定到我的模型。每个集合视图项目都有一个按钮和一个标签。我处理的点击动作,我有发件人事件参数,但我无法区分一个按钮其他。大多数其他没有处理集合视图的问题说使用标记属性,但这不会暴露在Interface Builder的绑定选项卡上。有一个 Argument Argument2 绑定,但是它们似乎不符合 / code>属性在objc代码,我不知道如何以其他方式访问这些参数。

I have a NSCollectionView (OS X, not iOS) bound to my model. Each collection view item has a button and a label. I'm handling the click actions and I have the sender and event arguments but I unable to distinguish one button from the others. Most other questions not dealing with Collection Views say to use the tag property, but this isn't exposed on the Interface Builder's bindings tab. There is an Argument and Argument2 bindings, but they dont seems to correspond to the tag property in the objc code and I don't know how to otherwise access these Arguments.

-(void)image_click:(id)sender forEvent:(NSEvent *)event
{
    NSButton *btn = sender;
    NSLog(@"image clicked, %ld", (long)btn.tag);   //image clicked, 0
}

如何区分Objective-C

How do I differentiate between buttons in Objective-C code inside the click actions of a bunch of buttons in a collection view?

推荐答案

在您的项目中添加一个名为MyModel的模型并声明属性uniqueID in
MyModel.h

Add a Model in your project named MyModel and declare property uniqueID in MyModel.h

@interface MyModel:NSObject  
@property (retain) NSString* unqiueID;  
@end  

MyModel.m

MyModel.m

@implementation MyModel  
@synthesize uniqueID=_uniqueID;
@end



在AppDelegate.m中创建一些模型对象并将它们添加到数组中

In AppDelegate.m create some model objects and add them into an array

在IB中添加一个ArrayController并将其绑定到AppDelegate中的数组声明

In IB add an ArrayController and bind it to the array declare in AppDelegate

在IB中选择CollectionView并绑定内容属性到ArrayController并将其ControllerKey属性设置为arrangeObjects

In IB select CollectionView and bind its Content property to ArrayController and set its ControllerKey property to arrangedObjects

在您的模板视图中使用NSButton的Target和Argument绑定发送唯一的参数到指定的选择器

In your Template View Use NSButton's Target and Argument bindings to send unique arguments to the selector specified

您的参数绑定应如下所示

绑定到:控制器视图项

模型键路径:resentObject.uniqueID

选择器名称:buttonClicked:

Your Arguments binding should look like this
Bind to: Controller View Item
Model Key Path: representedObject.uniqueID
Selector Name: buttonClicked:

和目标绑定

绑定到:应用程序委托

模型键路径:self

选择器名称:buttonClicked:

and Target bindings
Bind to: App Delegate
Model Key Path: self
Selector Name: buttonClicked:

这些步骤将在以下教程中详细解释。

https://developer.apple.com/ library / mac / documentation / cocoa / Conceptual / CollectionViews / Introduction / Introduction.html#// apple_ref / doc / uid / TP40009030

希望这有助于

The steps are explained in detail in the following tutorial
https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CollectionViews/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009030
Hope this helps

这篇关于如何处理来自NSCollectionView的按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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