如何使performSelector:withObject:afterDelay工作?

时间:2011-07-18 06:35:38

标签: iphone objective-c nsobject

以下是代码:

-(void)setProjectID:(NSString *)newProject {
    [self willChangeValueForKey:@"projectID"];
    [projectID release];
    projectID = [newProject copy];
    [self didChangeValueForKey:@"projectID"];

    // Since we have an ID, now we need to load it
    NSInvocation *returnInvocation = [NSInvocation invocationWithMethodSignature:
                                      [Detail instanceMethodSignatureForSelector:@selector(configureView:)]];
    [returnInvocation setTarget:self];
    [returnInvocation performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];
    [returnInvocation setSelector:@selector(configureView:)];
    [returnInvocation retainArguments];

    fetch = [[WBWDocumentFetcher alloc] init];
    [fetch retrieveDocument:[NSURL wb_URLForTabType:PROJECT_DETAILS inProject:projectID] returnBy:returnInvocation];
}

-(void)displayAlert
{
    UIAlertView * alert = [[UIAlertView alloc]
                           initWithTitle:@"Connection Error" 
                           message:@"Error loading Data."
                           delegate:self cancelButtonTitle:@"OK" 
                           otherButtonTitles:nil];
    [alert show];
    [alert release];
}

该应用程序崩溃说NSInvalidArguementException。 - [NSInvocation displayAlert]:无法识别的选择器发送到实例0x5842320 请帮忙!!!

4 个答案:

答案 0 :(得分:1)

我猜代码应该是这样的:

NSInvocation *returnInvocation = [NSInvocation invocationWithMethodSignature:
                                  [Detail instanceMethodSignatureForSelector:@selector(displayAlert)]];

[returnInvocation setTarget:self];
[returnInvocation setSelector:@selector(displayAlert)];
[returnInvocation invoke];

或简单地说:

[self performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];

答案 1 :(得分:0)

请勿使用withObject,只需使用PerformSelector:afterDelay:

另外,您应该在self上致电,而不是returnInvocation

答案 2 :(得分:0)

[self performSelector:@selector(displayAlert) withObject: message afterDelay:0.5];

试试这个..

答案 3 :(得分:0)

对调用它的对象执行

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

因此,如果您在returnInvocation上调用它,您将会收到无法识别的选择器错误,因为NSInvocation没有displayAlert方法。

使用

[self performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];

由于自己有方法。