如何实现与多个参数和afterDelay performSelector?
我是一个iOS新手。 我有一个select器方法如下 –
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second { }
我正在尝试执行这样的事情 –
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second" afterDelay:15.0];
但是那给了我一个错误 –
Instance method -performSelector:withObject:withObject:afterDelay: not found
任何想法,我所缺less的?
就我个人而言,我认为更接近您的需求的解决scheme是使用NSInvocation。
像下面这样的工作将会完成这项工作:
indexPath和dataSource是在同一个方法中定义的两个实例variables。
SEL aSelector = NSSelectorFromString(@"dropDownSelectedRow:withDataSource:"); if([dropDownDelegate respondsToSelector:aSelector]) { NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[dropDownDelegate methodSignatureForSelector:aSelector]]; [inv setSelector:aSelector]; [inv setTarget:dropDownDelegate]; [inv setArgument:&(indexPath) atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation [inv setArgument:&(dataSource) atIndex:3]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation [inv invoke]; }
因为不存在[NSObject performSelector:withObject:withObject:afterDelay:]
方法。
你需要将你想要发送的数据封装到一个Objective C对象中(例如NSArray,NSDictionary,一些自定义的Objective Ctypes),然后通过[NSObject performSelector:withObject:afterDelay:]
方法知名和爱。
例如:
NSArray * arrayOfThingsIWantToPassAlong = [NSArray arrayWithObjects: @"first", @"second", nil]; [self performSelector:@selector(fooFirstInput:) withObject:arrayOfThingsIWantToPassAlong afterDelay:15.0];
您可以将您的参数打包到一个对象中,并使用助手方法像Michael一样调用原始方法,现在已经有人build议了。
另一个选项是dispatch_after,它将在某个时间取得一个块并排队。
double delayInSeconds = 15.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self fooFirstInput:first secondInput:second]; });
或者,你已经发现,如果你不需要延迟,你可以使用- performSelector:withObject:withObject:
最简单的select是修改你的方法来获取包含两个参数的单个参数,比如一个NSArray
或者NSDictionary
(或者添加第二个方法,它接受一个参数,解包并调用第一个方法,然后调用第二个方法拖延)。
例如,你可以有这样的东西:
- (void) fooOneInput:(NSDictionary*) params { NSString* param1 = [params objectForKey:@"firstParam"]; NSString* param2 = [params objectForKey:@"secondParam"]; [self fooFirstInput:param1 secondInput:param2]; }
然后打电话给你,你可以这样做:
[self performSelector:@selector(fooOneInput:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: @"first", @"firstParam", @"second", @"secondParam", nil] afterDelay:15.0];
- (void) callFooWithArray: (NSArray *) inputArray { [self fooFirstInput: [inputArray objectAtIndex:0] secondInput: [inputArray objectAtIndex:1]]; } - (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second { }
并用以下方式调用它:
[self performSelector:@selector(callFooWithArray) withObject:[NSArray arrayWithObjects:@"first", @"second", nil] afterDelay:15.0];
你可以在这里find提供的所有types的performSelector:方法:
有一堆的变化,但没有一个版本,需要多个对象以及延迟。 您需要将您的参数包装在NSArray或NSDictionary中。
- performSelector: - performSelector:withObject: - performSelector:withObject:withObject: – performSelector:withObject:afterDelay: – performSelector:withObject:afterDelay:inModes: – performSelectorOnMainThread:withObject:waitUntilDone: – performSelectorOnMainThread:withObject:waitUntilDone:modes: – performSelector:onThread:withObject:waitUntilDone: – performSelector:onThread:withObject:waitUntilDone:modes: – performSelectorInBackground:withObject:
我只是做了一些调整,需要调用原来的方法。 我所做的是制定一个协议,并把我的对象。 另一种方法是在一个类别中定义方法,但是需要抑制一个警告(#pragma clang diagnostic ignored“-Wincomplete-implementation”)。
我不喜欢NSInvocation的方式,太复杂。 让我们保持简单和干净:
// Assume we have these variables id target, SEL aSelector, id parameter1, id parameter2; // Get the method IMP, method is a function pointer here. id (*method)(id, SEL, id, id) = (void *)[vc methodForSelector:aSelector]; // IMP is just a C function, so we can call it directly. id returnValue = method(vc, aSelector, parameter1, parameter2);