开发者

iPhone - object does not support a known selector anymore on NSTimed call

开发者 https://www.devze.com 2023-02-28 16:56 出处:网络
I have set a timer to reset an attribute of an object : - (IBAction)open:(id)sender { int viewID = ((UIButton*)sender).tag;

I have set a timer to reset an attribute of an object :

- (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;
}

When entering the setLoadingNotFinished method, I have this crash :

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 mai开发者_开发知识库n + 121
    16  MyApp                               0x00002765 start + 53
)
terminate called after throwing an instance of 'NSException'

Why does this crash happens ?

I don't understand why it doesn't find the selector.

The loadFinished attribute is set, even the first test on this attribute works before calling the NSTimer method, and I use this attribute in many places.

page is not nil in the timed method.

The self.webViews is a retained NSMutableArray.


Please check out the code again. I corrected it. It had a syntax error.

The following should fix it:

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

The selector method receives the NSTimer as its argument, not the userInfo. Then, with the NSTimer you retrieve the object you previously set as the userInfo.

(In your original -setLoadingNotFinished, page was not the WebViewController, it was the NSTimer. And, setLoadFinished was being sent to the NSTimer, which did not recognize it.)

Note that the error message was telling you that the selector method was being sent to an object of class _NSCFTimer. That gives you a clue as to the nature of the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消