Collection View Button Action (Cocoa, Xcode) -


in xib file have collection view contains button. in awake nib have defined several of buttons different images. application looks finder, not perform actions yet.

what want now, when pressing each button different file opens (i use filemanager method). can in non-collection view type application, when using collection view not sure how attach different action each button.

this have, open same pdf.pdf every button, when want every button open different pdf.

@implementation appcontroller -(void) awakefromnib {   mybutton *button1 = [[mybutton alloc] init]; button1.image = [nsimage imagenamed:@"img1"];   mybutton *button2 = [[mybutton alloc] init]; button2.image = [nsimage imagenamed:@"img2"];  mybutton *button3 = [[mybutton alloc] init]; button3.image = [nsimage imagenamed:@"img3"];  _modelarray = [[nsmutablearray alloc] init]; [controller addobject:button1]; [controller addobject:button2]; [controller addobject:button3];}  - (ibaction)opencvfile:(id)sender {   nsfilemanager *filemanagercv = [[nsfilemanager alloc] init]; nsstring *filepath = @"users/tom/pdf.pdf";  if ([filemanagercv fileexistsatpath:filepath]) {     nslog(@"file exists");     [[nsworkspace sharedworkspace]openfile:filepath withapplication:@"finder"];} else {     nslog(@"file not exist");  } } 

could help? appreciated.

using bindings

first off, introduce property model class points controller.

for example, simple model class be:

@interface modeldata : nsobject  @property nsstring *name; @property id controller;  @end  @implementation modeldata @end 

initialize collection view's content:

// in controller modeldata *data1 = [modeldata new]; data1.name = @"apple"; data1.controller = self;  modeldata *data2 = [modeldata new]; data2.name = @"boy"; data2.controller = self;  [self.collectionview setcontent:@[data1, data2]]; 

in interface builder, select button in prototype collection view item, navigate bindings inspector, create bindings argument , target:

  • for argument, set bind to collection view item, set model key path property in model class can use uniquely identify button (e.g. representedobject.name), , set selector name action method's signature, buttonclicked:.

  • for target, set bind to collection view item, set model key path points controller class (where action method implemented), , set selector name same above.

enter image description here enter image description here

after these 2 bindings set up, action method in controller class called argument designated when button clicked.

// in controller - (ibaction)buttonclicked:(id)anarg {     nslog(@"%@", anarg); } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -