Ios NSRunLoop cancelperformselectors with target';它不工作了

Ios NSRunLoop cancelperformselectors with target';它不工作了,ios,nsrunloop,Ios,Nsrunloop,我有以下代码,但我没有得到预期的结果 #import "CancelPerformSelectorTestAppDelegate.h" @implementation CancelPerformSelectorTestAppDelegate @synthesize window; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

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

#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
#导入“CancelPerformSelectorTestAppDelegate.h”
@实现取消PerformSelectOrteStatAppDelegate
@合成窗口;
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
[WindowMakeKeyandVisible];
for(无符号整数i=0;i<10;i++){
NSTimeInterval waitThisLong=i;
[自执行选择器:@selector(foo)with object:nil afterDelay:waitThisLong];
}
[[NSRunLoop currentRunLoop]取消目标为self的性能选择器];
返回YES;
}
-(作废)富{
静态无符号整数时间调用=0;
++时间调用;
NSLog(@“%s:我在%d-st/nd/th次被调用”、\uu func\uuuu、timesCalled);
}
-(void)applicationWillResignActive:(UIApplication*)应用程序{}
-(void)applicationIDBecomeActive:(UIApplication*)应用程序{}
-(无效)应用程序将终止:(UIApplication*)应用程序{}
-(无效)应用程序IDReceiveMemoryWarning:(UIApplication*)应用程序{}
-(无效)解除锁定{
[窗口释放];
[super dealoc];
}
@结束
我预计该函数将被调用大约0次,如果CPU一天运行缓慢,可能会被调用1次

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

提前谢谢,非常感谢, 尼克

你想要这个:

[UIApplication cancelPreviousPerformRequestsWithTarget:self];
你想要这个:

[UIApplication cancelPreviousPerformRequestsWithTarget:self];

您想使用类方法+cancelPreviousPerformRequestsWithTarget取消请求:

比如说,

[NSObject cancelPreviousPerformRequestsWithTarget:self];

的“处理点击手势”部分有一个示例,您希望使用类方法+cancelPreviousPerformRequestsWithTarget取消请求:

比如说,

[NSObject cancelPreviousPerformRequestsWithTarget:self];

的“处理点击手势”部分中有一个例子,它也起作用,因为
UIApplication
继承了
NSObject
的方法,它也起作用,因为
UIApplication
继承了
NSObject