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
, setbind to
collection view item
, setmodel key path
property in model class can use uniquely identify button (e.g.representedobject.name
), , setselector name
action method's signature,buttonclicked:
.for
target
, setbind to
collection view item
, setmodel key path
points controller class (where action method implemented), , setselector name
same above.
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
Post a Comment