0

I'm trying to add new categories to the NSArrayController class: it can select the first and the last item. I did so:

#import "NSArrayController+selectEnds.h"

@implementation NSArrayController (selectEnds)

- (void)selectFirst:(id)sender {
    if (self.arrangedObjects !=nil){ BOOL ignore = [self setSelectionIndex:0];}
}
- (void)selectLast:(id)sender {
    if (self.arrangedObjects !=nil){ 
        NSUInteger lastItem = [self.arrangedObjects count]-1;
        BOOL ignore = [self setSelectionIndex:lastItem];}
}
@end

I get no errors, but I would like to put this object in IB, using a blue cube and binding buttons to its "selectFirst" and "selectLast" methods.

But I'm a bit lost: which standard object to start with? A standard ArrayController? And then, which class name to choose to have the new methods listed?

Thanks for your help…

CrimsonDiego
  • 3,616
  • 1
  • 23
  • 26
berfis
  • 417
  • 4
  • 17
  • Have you added the category, and the two declarations, to the interface also, or only to the implementation? – abarnert Jun 14 '12 at 23:27

1 Answers1

1

Since you didn't show NSArrayController+selectEnds.h (which is what IB actually looks at), just NSArrayController+selectEnds.m, it's hard to know exactly what you got wrong, but there's two plausible guesses.

First, if you want these new methods to be part of the interface of the class NSArrayController, you have to add them to the interface declaration, not just to the implementation.

Second, if you want Xcode (or IB) to know that these new methods are actions, you have to label them as such: in the interface, instead of marking them plain void methods, mark them IBAction methods. (In the implementation, you can do either; it doesn't matter.)

So, NSArrayController+selectEnds.h should be:

#import <Cocoa/Cocoa.h>
@interface NSArrayController (selectEnds)
- (IBAction)selectFirst:(id)sender;
- (IBAction)selectLast:(id)sender;
@end
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I feel stupid, I looked at the "selectNext" method and wrote: (void)selectFirst:(id)sender; - (void)selectLast:(id)sender; instead of (IBAction). It works now, and with the standard IB object. Thank you very much! – berfis Jun 15 '12 at 00:19
  • Oops, I missed this feature. Ok, clicked! – berfis Jun 16 '12 at 11:43