简体   繁体   中英

using nsinvocation to call alert method

Im trying to call showUIAlertView from myMethod but get an exception: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil' Also,please tell what to write as argument for setTarget in line "[invocation setTarget:self];", i wrote self as both the methods are in the same file. thanks!!

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

}

Tracking the problem based on the error messsage:

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

It means that [[self class] instanceMethodSignatureForSelector:selector]; returned nil for the showUIAlertView:title: selector. And why? Because your class simply doesn't implement a message corresponding to the selector - you're confusing labels and argument names in your method declaration:

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

should in fact be

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

Also, setting the target to self is fine - target is the object on which you want to call the selector/invocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM