0

我正在尝试使用带有一个 NSString 作为参数的选择器执行一种方法,但失败了。我见过很多这样的问题(和答案)并尝试了大多数解决方案但没有成功。请参阅下面的代码和错误:

MyMainView.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)编程的新手,所以任何关于编码约定和最佳实践的评论都将不胜感激(即使与我的问题无关,但与我的代码相关)

4

2 回答 2

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;
于 2011-04-19T23:24:36.243 回答
2

这种方法:

-(void)displayMessageOnTextView:(NSString *)message

有选择器displayMessageOnTextView:。参数本身的名称(message在您的情况下)不会出现在选择器中。(这应该有点直观:名称不一定会在您调用方法时出现,因此它不会出现在选择器中。)所以您需要使用@selector(displayMessageOnTextView:).

于 2011-04-19T23:25:14.870 回答