gpt4 book ai didi

ios - NSRunLoop cancelPerformSelectorsWithTarget 不工作

转载 作者:行者123 更新时间:2023-11-28 18:27:20 24 4
gpt4 key购买 nike

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

#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 次! :( 总是。我做错了什么,我怎样才能达到预期的结果?

非常感谢,尼克

最佳答案

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

例如,

[NSObject cancelPreviousPerformRequestsWithTarget:self];

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

关于ios - NSRunLoop cancelPerformSelectorsWithTarget 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3830383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com