91

我是一个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

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

4

10 回答 10

145

就个人而言,我认为更接近您需求的解决方案是使用 NSInvocation。

像下面这样的东西会做的工作:

indexPathdataSource是在同一个方法中定义的两个实例变量。

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];
}
于 2013-04-02T10:11:44.727 回答
97

因为没有[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];
于 2011-12-08T23:14:39.107 回答
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];

});

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

于 2011-12-08T23:25:08.957 回答
7

最简单的选择是将您的方法修改为采用包含两个参数的单个参数,例如NSArrayor NSDictionary(或添加第二个采用单个参数的方法,将其解包并调用第一个方法,然后在 a 上调​​用第二个方法延迟)。

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

- (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];
于 2011-12-08T23:23:37.447 回答
6
- (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];
于 2011-12-08T23:21:27.260 回答
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: 
于 2011-12-08T23:24:18.003 回答
2

我不喜欢 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 *)[target methodForSelector:aSelector];

// IMP is just a C function, so we can call it directly.
id returnValue = method(target, aSelector, parameter1, parameter2);
于 2016-10-31T09:47:17.277 回答
1

我只是做了一些调整,需要调用原始方法。我所做的是制定一个协议并将我的对象投射到它上面。另一种方法是在类别中定义方法,但需要抑制警告(#pragma clang diagnostic ignored "-Wincomplete-implementation")。

于 2013-03-27T09:41:26.643 回答
0

一种简单且可重用的方法是扩展NSObject和实现

- (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments;

就像是:

- (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments
{
    NSMethodSignature *signature = [self methodSignatureForSelector: aSelector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
    [invocation setSelector: aSelector];

    int index = 2; //0 and 1 reserved
    for (NSObject *argument in arguments) {
        [invocation setArgument: &argument atIndex: index];
        index ++;
    }
    [invocation invokeWithTarget: self];
}
于 2019-05-02T14:51:52.930 回答
0

我将创建一个自定义对象,将我的所有参数作为属性,然后使用该单个对象作为参数

于 2019-05-02T14:55:23.423 回答