如何使 PerformSelector:withObject:afterDelay 工作?

发布于 2024-11-24 08:41:08 字数 1338 浏览 1 评论 0原文

代码如下:

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜声 2024-12-01 08:41:08

我想代码应该像:

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

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];
人海汹涌 2024-12-01 08:41:08

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

另外,您应该在 self 上调用此函数,而不是 returnInitation >

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

Also, you should call this on self, not returnInvocation

折戟 2024-12-01 08:41:08
[self performSelector:@selector(displayAlert) withObject: message afterDelay:0.5];

试试这个..

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

Try this..

陌路终见情 2024-12-01 08:41:08

- (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 a displayAlert method.

Use

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

As the self has the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文