I want to invoke setEditing:animated:
on a table view with a slight delay. Normally, I'd use performSelector:withObject:afterDelay:
but
setEditing:animated:
is a primitive BOOL - not an objectIn the past I would create a dummy method in my own class, like setTableAnimated
and then call [self performSelector:@selector(setTableAnimated) withObject:nil afterDelay:0.1f
but that feels kludgy to me.
Is there a better way to do it?
Why not use a dispatch_queue ?
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[tableView setEditing …];
});
You need to use NSInvocation
:
See this code, taken from this answer, and I've changed it slightly to match your question:
BOOL yes = YES;
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self.tableView methodSignatureForSelector:@selector(setEditing:Animated:)]];
[inv setSelector:@selector(setEditing:Animated:)];
[inv setTarget:self.tableView];
[inv setArgument:&yes atIndex:2]; //this is the editing BOOL (0 and 1 are explained in the link above)
[inv setArgument:&yes atIndex:3]; //this is the animated BOOL
[inv performSelector:@selector(invoke) withObject:nil afterDelay:0.1f];
The selector setEditing:animated:
is not compatible with performSelector:withObject:afterDelay
. You can only call methods with 0 or 1 arguments and the argument (if any) MUST be an object. So your workaround is the way to go. You can wrap the BOOL value in an NSValue
object and pass it to your setTableAnimated
method.
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