from stackoverflow: http://stackoverflow.com/questions/7589644/why-doesnt-cancelpreviousrequestswithtarget-cancel-an-nstimer
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 20, 280, 30)];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(buttonClick:) userInfo:nil repeats:NO];
[self performSelector:@selector(buttonClick:) withObject:@"Hello" afterDelay:6.0f];
// withObject:的参数应该和cancelPreviousPerformRequestsWithTarget:selector:object:的object:参数相同,这个参数可以填为nil
}
- (void)buttonClick:(id)sender
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonClick:) object:@"Hello"];
// [NSObject cancelPreviousPerformRequestsWithTarget:self];
// use this method cancel NSTimer is invalid
static int i = 0;
NSLog(@"%d", i++);
}
@end