我正在尝试使用带有一个NSString作为参数的选择器来执行方法并且失败。我已经看到很多这样的问题(和答案)并尝试了大多数解决方案但没有成功。请参阅以下代码和错误:
我的MainView.m
是我的视图控制器,它有一个方法:
-(void)displayMessageOnTextView:(NSString *)message
{
[tv1 setText:message];
}
在MainView.h
:
@interface MainView : UIViewController {
UITextView *tv1;
}
-(void)displayMessageOnTextView:(NSString *)message;
....
我有另一个由MainView触发的线程(来自MainView.m
):
- (void) runTest
{
MyThread *t = [[MyThread alloc] initWithInt:13253];
[t setMainView:self];
[t run];
[t release];
}
和MyThread.m
:
#import "MyThread.h"
#import "MainView.h"
@implementation MyThread
MainView *_view;
-(id)initWithInt:(int)someInt{
_view = NULL;
return self;
}
-(void)setMainView:(MainView*)view
{
_view = view;
}
-(int)run{
NSString *s = [NSString stringWithFormat:@"Display on Gui:%d", 1];
[_view performSelectorOnMainThread:@selector(displayMessageOnTextView:message:) withObject:s waitUntilDone:YES];
}
@end
错误:
2011-04-20 02:08:11.553 myApp[22627:207] -[MainView displayMessageOnTextView:message:]: unrecognized selector sent to instance 0x4e383a0
2011-04-20 02:08:11.555 myApp[22627:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainView displayMessageOnTextView:message:]: unrecognized selector sent to instance 0x4e383a0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc55a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f19313 objc_exception_throw + 44
2 CoreFoundation 0x00dc70bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d36966 ___forwarding___ + 966
4 CoreFoundation 0x00d36522 _CF_forwarding_prep_0 + 50
5 Foundation 0x0079e94e __NSThreadPerformPerform + 251
6 CoreFoundation 0x00da68ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
7 CoreFoundation 0x00d0488b __CFRunLoopDoSources0 + 571
8 CoreFoundation 0x00d03d86 __CFRunLoopRun + 470
9 CoreFoundation 0x00d03840 CFRunLoopRunSpecific + 208
10 CoreFoundation 0x00d03761 CFRunLoopRunInMode + 97
11 GraphicsServices 0x00ffd1c4 GSEventRunModal + 217
12 GraphicsServices 0x00ffd289 GSEventRun + 115
13 UIKit 0x00025c93 UIApplicationMain + 1160
14 myApp 0x000024c9 main + 121
15 myApp 0x00002445 start + 53
)
terminate called after throwing an instance of 'NSException'
注意:
我是Objective-C(通常是iOS)编程的新手,因此任何关于编码约定和最佳实践的评论都会受到赞赏(即使与我的问题无关,但与我的代码有关)
答案 0 :(得分:3)
您的方法已声明为
-(void)displayMessageOnTextView:(NSString *)message;
这意味着方法名称为displayMessageOnTextView:
,相应的选择器为@selector(displayMessageOnTextView:)
。请注意,方法名称不包含参数(变量)名称。
在-run
方法中,更改
[_view performSelectorOnMainThread:@selector(displayMessageOnTextView:message:)
withObject:s
waitUntilDone:YES];
到
[_view performSelectorOnMainThread:@selector(displayMessageOnTextView:)
withObject:s
waitUntilDone:YES];
并且不应再发生运行时错误。
对于记录,对应于displayMessageOnTextView:message:
的方法声明将是:
- (void)displayMessageOnTextView:(type1)parameter1 message:(type2)parameter2;
答案 1 :(得分:2)
此方法:
-(void)displayMessageOnTextView:(NSString *)message
有选择器displayMessageOnTextView:
。参数本身的名称(在您的情况下为message
)不会出现在选择器中。 (这应该是直观的:当你调用方法时,名称不一定会出现,因此它不会出现在选择器中。)所以你需要使用@selector(displayMessageOnTextView:)
。