- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"我在玩手机"); NSLog(@"手机没电了"); [self chargeMyIphone:^{ NSLog(@"出门逛街"); }]; NSLog(@"我在看电视"); } -(void)chargeMyIphone:(void(^)(void))finishBlock { double delayInSeconds = 10.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ NSLog(@"电充好了"); finishBlock(); }); }
(void(^)(void))finishBlock 第一个void表示此block无返回值。(^)为block type的标志。第二个(void)表示这个block无参数。finishBlock就是他的名字。无参数无返回类型的匿名函数就是我们的最简单的block了!他非常方便我们用来回调,因为他没有返回值,没有参数,就相当于只有内部的可执行代码!
performSelector: 和dispatch_time 是iOS中的多线程
参考:http://blog.csdn.net/mobanchengshuang/article/details/11751671