How to make performSelector:withObject:afterDelay work?

人盡茶涼 提交于 2019-12-13 08:29:09

问题


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!!!


回答1:


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

Also, you should call this on self, not returnInvocation




回答2:


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



回答3:


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

Try this..




回答4:


- (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.



来源:https://stackoverflow.com/questions/6729344/how-to-make-performselectorwithobjectafterdelay-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!