I am currently making an objective c program in which there are 4 shapes on the screen. The shapes must be highlighted based on a sequence of ints, and only one shape must be highlighted at a time. Thus, I must add a delay after each shape has been highlighted. Using 'sleep' causes the program to have very strange behavior, and the only other way I can think of delaying it would be using 'performSelector:withObject:afterDelay:' but the the method I wish to pass as the selector takes a number of arguments, which it will not allow.
So my question is this: is it possible to pass a method with a number of arguments into 'performSelector:withObject:afterDelay?' If not, what other ways are there to add a delay to the program without sleeping a thread?
Any help is appreciated.
Pass all arguments in NSDictionary
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Object1", @"key1", @"Object2", @"key2", nil];
//performSelector:withObject:dict afterDelay
// [self performSelector:@selector(aSelector) withObject:dict afterDelay:(NSTimeInterval)delay ];
-(void)aSelector : (NSDictionary *)dict
{
id lFirstArg = [dict objectForKey:@"Key1"]
...
}
If you dont want to change your method Signature, use NSInvocation
. Take a look at this SEL performSelector and arguments post.
-(void)myMethodWith:(int)number andBOOL:(BOOL) someBool andStr:(NSString *)str{
NSLog(@"%d %d %@",number,someBool,str);
}
-(void) testMethod{
SEL sel = @selector(myMethodWith:andBOOL:andStr:);
int i = 10;
BOOL bol = YES;
NSString *str = @"Hello !";
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:sel]];
[inv setSelector:sel];
[inv setTarget:self];
[inv setArgument:&i atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
[inv setArgument:&bol atIndex:3];
[inv setArgument:&str atIndex:4];
[inv performSelector:@selector(invoke) withObject:nil afterDelay:30];
}