2011-04-25 78 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的對象。這就給了你一個線索,錯誤的性質。