[英]How do I call NSManaged object's @dynamic method from from another object using a @selector and NSInvocation?
[英]using nsinvocation to call alert method
我试图从myMethod调用showUIAlertView但得到一个异常:由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ [NSInvocation invocationWithMethodSignature:]:方法签名参数不能为零'另外,请告诉写什么作为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];
}
根据错误消息跟踪问题:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil'
这意味着[[self class] instanceMethodSignatureForSelector:selector];
为showUIAlertView:title:
选择器返回nil
。 又为什么呢? 因为您的类根本没有实现与选择器相对应的消息-您在方法声明中混淆了标签和参数名称:
- (void)showUIAlertView:(NSString*)title:(NSString*)message;
实际上应该是
- (void)showUIAlertView:(NSString*)title title:(NSString*)message;
另外,将target设置为self
很好target
是要在其上调用选择器/调用的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.