iPhone - 对象在 NSTimed 调用中不再支持已知选择器

发布于 2024-11-03 13:38:37 字数 2662 浏览 0 评论 0原文

我设置了一个计时器来重置对象的属性:

- (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 方法之前对该属性进行的第一次测试也有效,而且我在很多地方都使用了该属性。
定时方法中 page 不为 nil。
self.webViews 是一个保留的 NSMutableArray。

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 main + 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-11-10 13:38:37

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

以下应该修复它:

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

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

(在您原来的 -setLoadingNotFinished 中,页面不是 WebViewController,而是 NSTimer。并且,setLoadFinished 被发送到 NSTimer,而 NSTimer 无法识别它。)

请注意,错误消息告诉您正在发送选择器方法到类 _NSCFTimer 的对象。这为您提供了有关错误性质的线索。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文