使用NSThread中的多个参数调用一个方法

时间:2011-07-04 09:38:57

标签: objective-c ios xcode nsthread

如何从NSThread调用一个方法?

我可以调用以下方法:

[regionsMapView addAnnotation:addAnnotation];

我如何在NSThread中调用它? 我用过如下:

[NSThread detachNewThreadSelector:@selector(addAnnotation) 
                         toTarget:regionsMapView 
                       withObject:addAnnotation];

但是应用程序崩溃了,如下所示:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSThread initWithTarget:selector:object:]: target does not implement selector (*** -[MKMapView addAnnotation])'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x0182b5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0197f313 objc_exception_throw + 44
    2   CoreFoundation                      0x017e3ef8 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x017e3e6a +[NSException raise:format:] + 58
    4   Foundation                          0x0006c392 -[NSThread initWithTarget:selector:object:] + 146
    5   Foundation                          0x0006c2d3 +[NSThread detachNewThreadSelector:toTarget:withObject:] + 98
    6   MJA                                 0x00008a8e -[TaetPaViewController showAddress:withItem:] + 389
    7   MJA                                 0x00008eb9 -[TaetPaViewController UpdateInfo] + 784
    8   MJA                                 0x0000835a -[TaetPaViewController parseTheLocalXml] + 408
    9   Foundation                          0x0006ccf4 -[NSThread main] + 81
    10  Foundation                          0x0006cc80 __NSThread__main__ + 1387
    11  libSystem.B.dylib                   0x911a47fd _pthread_start + 345
    12  libSystem.B.dylib                   0x911a4682 thread_start + 34
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

1 个答案:

答案 0 :(得分:3)

你最后忘记了结肠。

@selector(addAnnotation)

不是

@selector(addAnnotation:)

第一个调用不带参数的方法,第二个调用一个参数。 你的代码应该是这个

[NSThread detachNewThreadSelector:@selector(addAnnotation:) 
                     toTarget:regionsMapView 
                   withObject:addAnnotation];