我有以下代码,但我没有得到我期望的结果。
#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。我做错了什么,我如何才能达到我期望的结果?
先谢谢你,谢谢你,尼克
发布于 2010-09-30 20:47:22
您希望使用NSObject类方法+cancelPreviousPerformRequestsWithTarget来取消请求:
例如,
[NSObject cancelPreviousPerformRequestsWithTarget:self];在Event Handling Guide for multitouch events的“处理点击手势”部分有一个例子
发布于 2010-09-30 20:41:10
你想要这个:
[UIApplication cancelPreviousPerformRequestsWithTarget:self];https://stackoverflow.com/questions/3830383
复制相似问题