NSRunLoop cancelPerformSelectorsWithTarget 不起作用

发布于 2024-09-25 05:38:14 字数 1216 浏览 0 评论 0原文

我有以下代码,但没有得到我预期的结果。

#import "CancelPerformSelectorTestAppDelegate.h"
@implementation CancelPerformSelectorTestAppDelegate
@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [window makeKeyAndVisible];
    for(unsigned int i = 0; i < 10; i++){
        NSTimeInterval waitThisLong = i;
        [self performSelector:@selector(foo) withObject:nil afterDelay: waitThisLong];
    }

    [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: self];

    return YES;
}

- (void) foo {
    static unsigned int timesCalled = 0;
    ++timesCalled;
    NSLog(@"%s: I am called for the %d-st/nd/th time", __func__, timesCalled);
}

- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {}
- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

我预计该函数将被调用大约 0 次,如果 CPU 运行缓慢,则可能调用 1 次。

该函数将执行 10 次! :( 总是。我做错了什么,我怎样才能达到我预期的结果?

预先感谢,非常感谢, 缺口

I have this following code and I am not getting the results I expected.

#import "CancelPerformSelectorTestAppDelegate.h"
@implementation CancelPerformSelectorTestAppDelegate
@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [window makeKeyAndVisible];
    for(unsigned int i = 0; i < 10; i++){
        NSTimeInterval waitThisLong = i;
        [self performSelector:@selector(foo) withObject:nil afterDelay: waitThisLong];
    }

    [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: self];

    return YES;
}

- (void) foo {
    static unsigned int timesCalled = 0;
    ++timesCalled;
    NSLog(@"%s: I am called for the %d-st/nd/th time", __func__, timesCalled);
}

- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {}
- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

I expected the function to be called about 0 times, perhaps 1 if the CPU is having a slow day.

The function will execute 10 times! :( Always. What am I doing wrong, and how could I achieve the results I expected?

Thanks in advance, a lot,
Nick

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

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

发布评论

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

评论(2

↙厌世 2024-10-02 05:38:14

您想要使用 NSObject 类方法 +cancelPreviousPerformRequestsWithTarget:

例如,

[NSObject cancelPreviousPerformRequestsWithTarget:self];

多点触控事件的事件处理指南

You want to cancel the request using the NSObject class method +cancelPreviousPerformRequestsWithTarget:

For example,

[NSObject cancelPreviousPerformRequestsWithTarget:self];

There's an example in the "handling tap gestures" section of the Event Handling Guide for multitouch events

遗忘曾经 2024-10-02 05:38:14

你想要这个:

[UIApplication cancelPreviousPerformRequestsWithTarget:self];

You want this:

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