【问题标题】:How to handle a button click from NSCollectionView如何处理来自 NSCollectionView 的按钮单击
【发布时间】:2013-06-26 17:12:53
【问题描述】:

我有一个NSCollectionView (OS X,不是 iOS) 绑定到我的模型。每个集合视图项都有一个按钮和一个标签。我正在处理点击操作,我有 senderevent 参数,但我无法区分一个按钮和其他按钮。大多数其他不涉及集合视图的问题都说使用tag 属性,但这并未在Interface Builder 的绑定选项卡上公开。有一个 ArgumentArgument2 绑定,但它们似乎与 objc 代码中的 tag 属性不对应,我不知道如何访问这些参数。

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

如何区分集合视图中一堆按钮的单击操作中的 Objective-C 代码中的按钮?

【问题讨论】:

标签: objective-c xcode cocoa interface-builder


【解决方案1】:

我假设您想确定视图中的按钮所代表的模型对象。我能够通过遍历集合视图中的按钮来确定模型对象。我无法使用选择索引或任何其他类似属性,但最终可以确定模型。

假设您的 NSArrayController 已经拥有您的数组,则执行以下操作:

绑定:

Collection View 只需要一个绑定

Bind to:         <NSArrayController instance>
Controller Key:  arrangedObjects
Model Key Path:  <blank>

控制器:

您应该将控制器连接到内容视图

property (weak) IBOutlet NSCollectionView *collectionView;

最后,接收按钮点击消息的控制器应该实现这个IBAction:

- (IBAction) collectionViewClick:(id)sender
{  
  id objectInClickedView = nil;

  for( int i = 0; i < [[self.collectionView content] count]; i++ ) {
    NSCollectionViewItem *viewItem = [self.collectionView itemAtIndex:i];

    if( [sender isDescendantOf:[viewItem view]] ) {
      objectInClickedView = [[self.collectionView content] objectAtIndex:i];
    }
  }
}

这会将对象分配给objectInClickedView。如果你真的对 view 或 viewItem 感兴趣,可以修改代码。

【讨论】:

    【解决方案2】:

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

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

    MyModel.m

    @implementation MyModel  
    @synthesize uniqueID=_uniqueID;
    @end
    

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

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

    在 IB 中选择 CollectionView 并将其 Content 属性绑定到 ArrayController 并将其 ControllerKey 属性设置为排列对象

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

    您的 Arguments 绑定应如下所示
    绑定到:控制器视图项
    模型键路径:representedObject.uniqueID
    选择器名称:buttonClicked:

    和目标绑定
    绑定到:应用委托
    模型键路径:self
    选择器名称:buttonClicked:

    步骤在下面的教程中有详细说明
    https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CollectionViews/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009030
    希望这会有所帮助

    【讨论】:

    • 您应该在 AppDelegate.m 中声明选择器 -(void)buttonClicked:(id)object 如果您正确设置绑定,那么 object 将是您的唯一参数
    • OOOOOH。这就说得通了。我试试看。
    • 您确定教程的链接正确吗?这只是 NSCollectionView 类参考,不是教程,也没有讨论 binging 参数或目标。
    【解决方案3】:

    我会这样做(因为你要按下的按钮应该与对应的模型耦合,因此表示的对象):

    1. 为你的 collectionViewItem 的模型添加一个方法(例如 buttonClicked)
    2. 将按钮目标绑定到集合视图项
    3. 绑定时将模型键路径设置为:representedObject
    4. 在绑定时将选择器名称设置为:您之前选择的方法名称(例如 buttonClicked)
    5. 如果您必须告诉委托,请将协议添加到您的模型中

    【讨论】:

    • 这比其他一些建议的方法更简单、更直接。在代码中,您定义了一个自定义对象,该对象将保存您要在集合视图中显示的数据,并且还将实现所需的按钮操作,例如,- (IBAction)buttonClicked:(id)sender。在 Interface Builder 中,将 NSCollectionView 内容绑定到这些对象的数组,并将按钮的目标绑定到 CollectionViewItem >代表对象 > buttonClicked:.
    【解决方案4】:

    根据这个“//image clicked, 0”,我认为你每次点击按钮都会得到 0,对吗?

    如果是这样,您可以在 collectioViewItem 中为按钮设置一个出口并覆盖 -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 设置/增加按钮的每个实例的标签。

    【讨论】:

      【解决方案5】:

      在 cellForItemAtIndexPath 方法中使用它

         [[cell myButton] addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside];
      
      
         -(IBAction)myClickEvent:(id)sender event:(id)event {
      
                NSSet *touches = [event allTouches];
      
                UITouch *touch = [touches anyObject];
      
                CGPoint currentTouchPosition = [touch locationInView:_myCollectionArray];
      
                NSIndexPath *indexPath = [_myCollectionArray indexPathForItemAtPoint: currentTouchPosition];
      
         }
      

      【讨论】:

        猜你喜欢
        • 2016-05-12
        • 2019-12-26
        • 1970-01-01
        • 2020-07-23
        • 2011-10-16
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        相关资源
        最近更新 更多