首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSRunLoop cancelPerformSelectorsWithTarget不工作

NSRunLoop cancelPerformSelectorsWithTarget不工作
EN

Stack Overflow用户
提问于 2010-09-30 20:16:02
回答 2查看 1.4K关注 0票数 0

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

代码语言:javascript
复制
#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次!:( Always。我做错了什么,我如何才能达到我期望的结果?

先谢谢你,谢谢你,尼克

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-30 20:47:22

您希望使用NSObject类方法+cancelPreviousPerformRequestsWithTarget来取消请求:

例如,

代码语言:javascript
复制
[NSObject cancelPreviousPerformRequestsWithTarget:self];

Event Handling Guide for multitouch events的“处理点击手势”部分有一个例子

票数 5
EN

Stack Overflow用户

发布于 2010-09-30 20:41:10

你想要这个:

代码语言:javascript
复制
[UIApplication cancelPreviousPerformRequestsWithTarget:self];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3830383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档