如何使 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。 -[NSInspiration displayAlert]:无法识别的选择器发送到实例 0x5842320 请帮忙!!!
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!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想代码应该像:
或简单地:
I guess code should be like :
or simply:
不要使用
withObject
,只需使用PerformSelector:afterDelay:
另外,您应该在
self
上调用此函数,而不是returnInitation
>Don't use
withObject
, just usePerformSelector:afterDelay:
Also, you should call this on
self
, notreturnInvocation
试试这个..
Try this..
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
在调用它的对象上执行。因此,如果您在
returnInitation
上调用它,您将收到无法识别的选择器错误,因为 NSInitation 没有displayAlert
方法。使用
[self PerformSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];
因为 self 有该方法。
- (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 adisplayAlert
method.Use
[self performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];
As the self has the method.