Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@selector with multiple arguments

How does one call an @selector method with multiple arguments?

I have the following

[self performSelector:@selector(changeImage:withString:) withObject:A1 withObject:fileString2 afterDelay:0.1];

but get an

unrecognized selector sent to instance

error

My method I am calling is as follows

-(void) changeImage: (UIButton *) button withString: (NSString *) string
{
[button setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
}
like image 708
some_id Avatar asked Dec 29 '10 15:12

some_id


5 Answers

You should use NSInvocation

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
                             [self methodSignatureForSelector:@selector(changeImage:withString:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(changeImage:withString:)];
[invocation setArgument:A1 atIndex:2];
[invocation setArgument:fileString2 atIndex:3];
[NSTimer scheduledTimerWithTimeInterval:0.1f invocation:invocation repeats:NO];
like image 150
Jilouc Avatar answered Oct 28 '22 17:10

Jilouc


The NSObject class has a performSelector:withObject:afterDelay: method, and the NSObject protocol specifies a performSelector:withObject:withObject: method, but nowhere is there specified a performSelector:withObject:withObject:afterDelay:.

In this case, you'll have to use an NSInvocation to get the functionality you desire. Set up the invocation, and then you can call performSelector:withObject:afterDelay on the invocation itself, using the selector invoke and a nil object.

like image 28
Endemic Avatar answered Oct 28 '22 18:10

Endemic


There is no method for performing a selector with multiple arguments and a delay. You could wrap the button and the string object in a NSDictionary to work around this like this:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:A1,@"button",fileString2,@"string",nil];
[self performSelector:@selector(changeWithDict:) withObject:dict afterDelay:0.1];
//...

-(void)changeWithDict:(NSDictionary *)dict {
    [[dict objectForKey:@"button"] setImage:[UIImage imageNamed:[dict objectForKey:@"string"]] forState:UIControlStateNormal];
}
like image 44
mrueg Avatar answered Oct 28 '22 18:10

mrueg


If you're targeting iOS 4.0+ you could use blocks. Something along the lines of this should do the trick.

// Delay execution of my block for 0.1 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, USEC_PER_SEC / 10ull), dispatch_get_current_queue(), ^{
    [self changeImage:A1 withString:fileString2];
});
like image 2
extremeboredom Avatar answered Oct 28 '22 18:10

extremeboredom


It's not a good way to get round it, but if you wanted you could modify the method to accept an NSArray, when the object at index 0 is the button and at index 1 is the string.

like image 1
Jonathan. Avatar answered Oct 28 '22 18:10

Jonathan.