0

我试图从 myMethod 调用 showUIAlertView 但得到一个异常:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+ [NSInvocation invocationWithMethodSignature:]:方法签名参数不能为 nil”另外,请告知要写什么作为 setTarget 的参数在“[invocation setTarget:self];”行中,我写了 self,因为这两种方法都在同一个文件中。谢谢!!

- (void)showUIAlertView:(NSString*)title:(NSString*)message
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title
                                                      message:message
                                                      delegate:nil 
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
    [alertView show];
}

- (void)myMethod:(NSString*)someData
{
    //some lines of code
    SEL selector = @selector(showUIAlertView:title:);
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    [invocation setSelector:selector];
     NSString *str1 = @"Status Message";
     NSString *str2 = @"You are not a member yet.";
     [invocation setTarget:self];
     [invocation setArgument:&str1 atIndex:2];
     [invocation setArgument:&str2 atIndex:3];
    [NSTimer scheduledTimerWithTimeInterval:0.1 invocation:invocation repeats:NO];

}

4

1 回答 1

0

根据错误消息跟踪问题:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil'

这意味着为选择器[[self class] instanceMethodSignatureForSelector:selector];返回。为什么?因为您的类根本没有实现与选择器相对应的消息 - 您在方法声明中混淆了标签和参数名称:nilshowUIAlertView:title:

- (void)showUIAlertView:(NSString*)title:(NSString*)message;

实际上应该是

- (void)showUIAlertView:(NSString*)title title:(NSString*)message;

此外,将目标设置self为很好 -target是您要调用选择器/调用的对象。

于 2012-09-05T14:45:25.277 回答