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];
}
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];
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.
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];
}
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];
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With