tags:

views:

177

answers:

1

I have problem with handler event. I need create handler with one NSString parameter. I try, but it doesn't work. Sample code:

@interface Example : NSObject {
    id target;
    SEL action;
}

- (id)initWithTarget:(id)targetObject action:(SEL)runAction;
- (void)activate;
@end

@implementation Example
- (id)initWithTarget:(id)targetObject action:(SEL)runAction {
    if (self = [super init]) {
        target = targetObject;
        action = runAction;
    }
    return self;
}

- (void)activate {
    [target performSelector:action withObject:self withObject: @"My Message"];
}
@end

@interface ExampleHandler : NSObject {

}

-(void):init;
-(void)myHandler:(NSString *)str;

@end

@implementation ExampleHandler

-(void)init {
    [super init];
    Example *ex = [[Example alloc] initWithTarget: self action: @selector(myHandler) ];

}

-(void)myHandler:(NSString *)str {
    NSLog(str);
}

@end

What should I change in my code that I have handler with one parameter?

+2  A: 

The method 'myHandler:' takes one argument, a string. Yet in your example, you are passing it two objects, 'self' and the string. You should change

[target performSelector:action withObject:self withObject:@"My Message"];

to

[target performSelector:action withObject:@"My Message"];

If on the other hand, you really want to pass 'self' to the method, change the myHandler method to something like:

-(void)myHandler:(id)example string:(NSString*)str

On a side note, your Example should either retain 'target' unless you have guarantees that the ExampleHandler will not get deallocated before the Example object.

bobDevil
It's also worth noting that the OP has used @selector(myHandler). Note the missing colon, it should be @selector(myHandler:).
Rob Keniger