2011-04-25 80 views
1

我都设置一个计时器,重置对象的属性支持一个已知的选择:iPhone对象已经不上NSTimed呼叫

- (IBAction)open:(id)sender { 
    int viewID = ((UIButton*)sender).tag; 

    for (int i=0; i<[self.webViews count]; i++) { 
     WebViewController* page = [self.webViews objectAtIndex:i]; 
     if (page.loadFinished == NO) { 
      [page.webView stopLoading]; 
      [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setLoadingNotFinished:) userInfo:page repeats:NO]; 
     } 
    } 


- (void) setLoadingNotFinished:(WebViewController*)page { 
    page.loadFinished = NO; 
} 

当进入setLoadingNotFinished方法,我有这样的崩溃:

2011-04-25 04:34:22.358 MyApp[7823:207] -[__NSCFTimer setLoadFinished:]: unrecognized selector sent to instance 0x4b203d0 
2011-04-25 04:34:22.360 MyApp[7823:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer setLoadFinished:]: unrecognized selector sent to instance 0x4b203d0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00dca5a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f1e313 objc_exception_throw + 44 
    2 CoreFoundation      0x00dcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00d3b966 ___forwarding___ + 966 
    4 CoreFoundation      0x00d3b522 _CF_forwarding_prep_0 + 50 
    5 MyApp        0x00003a42 -[MainGridController setLoadingNotFinished:] + 66 
    6 Foundation       0x007ba749 __NSFireTimer + 125 
    7 CoreFoundation      0x00dab8c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 
    8 CoreFoundation      0x00dace74 __CFRunLoopDoTimer + 1220 
    9 CoreFoundation      0x00d092c9 __CFRunLoopRun + 1817 
    10 CoreFoundation      0x00d08840 CFRunLoopRunSpecific + 208 
    11 CoreFoundation      0x00d08761 CFRunLoopRunInMode + 97 
    12 GraphicsServices     0x010021c4 GSEventRunModal + 217 
    13 GraphicsServices     0x01002289 GSEventRun + 115 
    14 UIKit        0x0002ac93 UIApplicationMain + 1160 
    15 MyApp        0x000027e9 main + 121 
    16 MyApp        0x00002765 start + 53 
) 
terminate called after throwing an instance of 'NSException' 

为什么会发生这种事故?
我不明白为什么它没有找到选择器。

的loadFinished属性设置,甚至在这个属性的第一次测试调用的NSTimer方法之前的作品,我在很多地方使用这个属性。
页面在定时方法中不是零。
self.webViews是一个保留的NSMutableArray。

+0

请注意,我在我的代码纠正语法错误。它现在是正确的,但如果你使用的原始帖子是错误的。 – 2011-04-25 01:23:20

回答

2

请再次查看代码。我纠正了它。它有一个语法错误。

以下应修复:

- (void) setLoadingNotFinished:(NSTimer *)myTimer { WebViewController *page = (WebViewController *)[myTimer userInfo]; page.loadFinished = NO; }

选择器方法接收的NSTimer作为它的参数,而不是用户信息。然后,使用NSTimer检索先前设置为userInfo的对象。

(在原来的-setLoadingNotFinished,页面不是WebViewController,这是的NSTimer。而且,setLoadFinished被发送到的NSTimer,不承认它。)

注意,错误消息告诉你选择器方法被发送到类_NSCFTimer的对象。这就给了你一个线索,错误的性质。