2011-07-18 73 views
-1

下面的代碼:如何使performSelector:withObject:afterDelay工作?

-(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 請幫忙!!!

回答

0

不要使用withObject,只需使用PerformSelector:afterDelay:

此外,你應該把這種對self,不returnInvocation

+0

仍然崩潰,並且它給出警告NSInvocation可能不會響應太performSelectpr:afterDelay – Ashutosh

+0

@ashutosh見上面 – PengOne

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]; 
0
[self performSelector:@selector(displayAlert) withObject: message afterDelay:0.5]; 

試試這個......

0

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay在調用它的對象上執行。

所以,如果你打電話給returnInvocation你會得到無法識別的選擇器錯誤,因爲NSInvocation沒有displayAlert方法。

使用

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

由於自身具有的方法。

相關問題