typedef void (^CallbackHandler)();
@interface ViewController ()
{
NSTimer* _blockTimer;
}
- (void)blockTest:(CallbackHandler)handler;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self blockTest:^(void) {
NSLog(@"Block処理実行");
}];
}
- (void)blockTest:(CallbackHandler)handler
{
NSLog(@"blockTest 開始");
_blockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:[NSBlockOperation blockOperationWithBlock:^(void) {
static NSInteger count = 0;
count++;
NSLog(@"%d", count);
if (count > 10) {
[_blockTimer invalidate];
_blockTimer = nil;
if (handler) handler();
}
}] selector:@selector(main) userInfo:nil repeats:YES];
}
@end
コメントする