0

1つの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)プログラミングに慣れていないので、コーディング規約とベストプラクティスに関するコメントをいただければ幸いです(私の質問に関連していなくても、私のコードに関連している場合でも)

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 に答える