开发者

How to make performSelector:withObject:afterDelay work?

Here's the code:

-(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];
}

The app is crashing saying NSInvalidArguementException. -[NSInvocation displayAlert]: unrecognized selector sent to instance 0x5842320 Please help!!!


I guess code should be like :

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

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

or simply:

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


Don't use withObject, just use PerformSelector:afterDelay:

Also, you should call this on self, not returnInvocation


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

Try this..


- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay is performed on the object that calls it.

So if you call it on returnInvocation you are going to get the unrecognized selector error because NSInvocation doesnt have a displayAlert method.

Use

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

As the self has the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜