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

Suc*_*chi 89 iphone selector ios performselector

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

- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second
{

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现这样的东西 -

[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second" afterDelay:15.0];
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误说 -

Instance method -performSelector:withObject:withObject:afterDelay: not found
Run Code Online (Sandbox Code Playgroud)

关于我缺少什么的任何想法?

val*_*ine 140

就个人而言,我认为更接近您的需求的解决方案是使用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];
}
Run Code Online (Sandbox Code Playgroud)

  • 你如何用这种技术指定延迟? (14认同)
  • @death_au,而不是`invoke`,调用:`[inv performSelector:@selector(invoke)withObject:nil afterDelay:1];`我必须同意这是一个很好的解决方案.大家快乐编码! (4认同)
  • 同意.这应该是正确的答案.非常有用的解决方 特别是在我的情况下,不允许更改包含多个参数的方法的签名. (2认同)
  • 有点迟到的谈话,但有一个问题.什么是dropDownDelegate? (2认同)

Mic*_*ann 98

因为没有[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];
Run Code Online (Sandbox Code Playgroud)

  • 这是一个单独的问题@Raj ...请单独发布. (2认同)

Fir*_*eer 34

您可以将参数打包到一个对象中,并使用辅助方法将原始方法称为Michael,现在其他人已建议.

另一个选项是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];

});
Run Code Online (Sandbox Code Playgroud)

或者,正如您已经发现的那样,如果您不需要延迟,您可以使用 - performSelector:withObject:withObject:


aro*_*oth 8

最简单的选择是修改你的方法以获取包含两个参数的单个参数,例如NSArrayNSDictionary(或者添加第二个接受单个参数的方法,解压缩它,调用第一个方法,然后调用第二个方法)延迟).

例如,你可以有类似的东西:

- (void) fooOneInput:(NSDictionary*) params {
    NSString* param1 = [params objectForKey:@"firstParam"];
    NSString* param2 = [params objectForKey:@"secondParam"];
    [self fooFirstInput:param1 secondInput:param2];
}
Run Code Online (Sandbox Code Playgroud)

然后打电话给你,你可以这样做:

[self performSelector:@selector(fooOneInput:) 
      withObject:[NSDictionary dictionaryWithObjectsAndKeys: @"first", @"firstParam", @"second", @"secondParam", nil] 
      afterDelay:15.0];
Run Code Online (Sandbox Code Playgroud)


Mik*_*ace 6

- (void) callFooWithArray: (NSArray *) inputArray
{
    [self fooFirstInput: [inputArray objectAtIndex:0] secondInput: [inputArray objectAtIndex:1]];
}


- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second
{

}
Run Code Online (Sandbox Code Playgroud)

并称之为:

[self performSelector:@selector(callFooWithArray) withObject:[NSArray arrayWithObjects:@"first", @"second", nil] afterDelay:15.0];
Run Code Online (Sandbox Code Playgroud)


Sti*_*sis 5

你可以在这里找到提供的所有类型的performSelector:方法:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html

有许多变化,但没有一个版本需要多个对象以及延迟.您需要在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: 
Run Code Online (Sandbox Code Playgroud)