iOS-如何使用多个参数和afterDelay实现performSelector?

我是iOS新手。我有一个选择器方法如下-


- (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

关于我所缺少的任何想法吗?


开满天机
浏览 873回答 3
3回答

红糖糍粑

我个人认为,更符合您需求的解决方案是使用NSInvocation。类似于以下内容的工作:indexPath和 dataSource是在同一方法中定义的两个实例变量。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];}

慕尼黑8549860

因为没有这种[NSObject performSelector:withObject:withObject:afterDelay:]方法。您需要将要发送的数据封装到某个单一的Objective C对象(例如,NSArray,NSDictionary,某些自定义Objective C类型)中,然后通过[NSObject performSelector:withObject:afterDelay:]众所周知的方法将其传递。例如:NSArray * arrayOfThingsIWantToPassAlong =     [NSArray arrayWithObjects: @"first", @"second", nil];[self performSelector:@selector(fooFirstInput:)            withObject:arrayOfThingsIWantToPassAlong             afterDelay:15.0];

炎炎设计

您可以将参数打包到一个对象中,并使用一个助手方法来调用您原来的方法,就像迈克尔和其他人建议的那样。另一个选项是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:
打开App,查看更多内容
随时随地看视频慕课网APP