timer

Objective-C Timer(实用)

 (2011-04-07 10:38:51)
标签: 

ipad

 

iphone

 

ios

 

it

 

objective-c

 

开发

 

移动

分类: iPhone


//I am creating a dictionary object that I can pass to my selector method.


NSArray *keys = [NSArray arrayWithObjects:@"key1"@"key2"@"key3"nil];


NSArray *objects = [NSArray arrayWithObjects:@"value1"@"value2"@"value3"nil];


NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];


[NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(fireTimer:) userInfo:myDictionary repeats:YES];


//The fireTimer() is the selector method I setup above


- (void) fireTimer:(NSTimer*)theTimer {

for (id key in [theTimer userInfo]) {

NSLog(@"key: %@, value: %@", key, [[theTimer userInfoobjectForKey:key]);

}

//kill the timer

[theTimer invalidate];

theTimer = nil;

}


 


//here is a similar example but with using an NSNumber


NSNumber *myInt = [NSNumber numberWithInt:4];


[NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(fireTimer:) userInfo:myInt repeats:YES];


- (void) fireTimer:(NSTimer*)theTimer {

NSLog(@" %i ", [[theTimer userInfo] integerValue]);

[theTimer invalidate];

theTimer = nil;

}

原文地址:https://www.cnblogs.com/moonvan/p/2248521.html