How to handle button clicks from NSCollectionView

I have an NSCollectionView (OS X, not iOS) attached to my model. Each item in the collection view has a button and a label. I handle click actions and I have sender and event arguments, but I cannot distinguish one button from the rest. Most of the other issues not related to Collection View say they use the tag property, but this does not appear on the binding tab of the Builder interface. There are Argument and Argument2 bindings, but they don't seem to match the tag property in objc code, and I don't know how to access these arguments in any other way.

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

How can I distinguish buttons in Objective-C code within the actions of clicking a button bundle in a collection view?

+6
source share
5 answers

Add a model to your project called MyModel and declare a uniqueID property in MyModel.h

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

MyModel.m

 @implementation MyModel @synthesize uniqueID=_uniqueID; @end 

In AppDelegate.m create some model objects and add them to the array

In IB, add an ArrayController and bind it to an array declaration in AppDelegate

In IB, select CollectionView and bind its Content property to ArrayController and set its ControllerKey property for ordered objects

In your template view, use NSButton Target and Argument object bindings to send unique arguments to the specified selector

The Argument binding should look like this: Bind to: Controller control Model key path: presentObject.uniqueID
Selector Name: buttonClicked:

and target binding
Bind to: application delegate
Model key path: self Selector name: buttonClicked:

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

+2
source

I assume that you want to define a model object represented by a button in a view. I was able to identify the model object by clicking on the buttons in the form of a collection. I could not use a selection index or any other similar attribute, but in the end, a model can be defined.

Assuming your NSArrayController already has your array, do the following:

Handcuffs:

Only one snap is required to view the collection.

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

Controller:

You must connect the controller to the content view.

 property (weak) IBOutlet NSCollectionView *collectionView; 

Finally, the controller receiving the message with the click of a button should implement this 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]; } } } 

An objectInClickedView will be assigned. If you are really interested in a view or viewItem, you can change the code.

+2
source

I would do it like this (because the button you want to click must be associated with the corresponding model, so the object presented):

  • Add a method to the model of your collectionViewItem (e.g. buttonClicked)
  • Bind the Target Button to Collection item.
  • When binding the specified model key path to: the represented object
  • When binding set selectorname to: methodname that you selected earlier (e.g. buttonClicked)
  • Add a protocol to your model if you must inform the delegate
+1
source

According to this "// clicked image, 0", I think you get 0 for each button and every click of the button, am I right?

If so, you can have a button exit in collectioViewItem and overide - (id) initWithNibName: (NSString *) nibNameOrNil bundle: (NSBundle *) nibBundleOrNil to set / increase the label of each button instance.

0
source

use this in cellForItemAtIndexPath method

  [[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]; } 
-one
source

All Articles