Problems passing and using selectors when making buttons

78 Views Asked by At

I am trying to do some coding in Xcode and am having some problems with selectors. I have created a subclass of NSButton called modifiedNSButton. One of the constructors is

+ (id)CreateButtonViewUsingObject:(id)targetObject  selector:(SEL)select caption:(NSString *)label;

However, the only way I have been able to get it to work is to pass the selector as @selector(bleep:) It works but I get the message

Incompatible pointer types sending 'NSString *' to parameter of type 'SEL'

I assume that this means that @selector(bleep:) is sent as an NSString instead of a SEL. However, it seems to work and I can't tell if I am doing something wrong. I will post my code to my Git for people to look at and then give a pointer to the location.

1

There are 1 best solutions below

4
Andreas Oetjen On

First, you really should follow the naming conventions in Objective C:

  • your Class should be named ModifiedNSButton, starting with a big letter
  • in contrary, functions should start with a small letter

Then, a "constructor" should also follow some conventions:

  • The return type should be instancetype, not id
  • If it's an initializer, start with init... (like initUsingObject...)
  • If it's a class factory, start the function with the name of the class.

In your case, you should name it + (instancetype) modifiedNSButtonUsingObject:(id)targetObject selector:(SEL)select caption:(NSString*)label

After all this said, I don't see a problem with your code:

@interface ModifiedNSButton : NSButton

@end

@implementation ModifiedNSButton

+ (instancetype)modifiedNSButtonUsingObject:(id)targetObject selector:(SEL)select caption:(NSString *)label {
    return [self new];
}

- (void) handler {
}

- (void) handlerWithName:(NSString*)name andValue:(NSInteger)value {
}

- (void) test {
    NSButton *b1 = [ModifiedNSButton modifiedNSButtonUsingObject:self selector:@selector(handler) caption:@"One"];
    NSButton *b2 = [ModifiedNSButton modifiedNSButtonUsingObject:self selector:@selector(handlerWithName:andValue:) caption:@"Two"];
}
@end

I don't see any "incompatible pointer type" warnings.