// Student.h #import <Foundation/Foundation.h> @interface Student : NSObject - (void)PlayBeforeExam:(void(^)(NSString*))TakingMakeUpExam Punish:(void(^)(void))CompletionHandler; @end // Student.m #import "Student.h" @implementation Student - (void)PlayBeforeExam:(void (^)(NSString *))TakingMakeUpExam Punish:(void(^)(void))CompletionHandler{ NSLog(@"play with my girlfriend!"); NSLog(@"one month later..."); /*方法执行完毕之后在某个时机调用block*/ TakingMakeUpExam(@"english"); CompletionHandler(); } @end - (void)viewDidLoad { [super viewDidLoad]; Student *Zhangsan = [Student new]; [Zhangsan PlayBeforeExam:^(NSString *English) { NSLog(@"Zhangsan is taking %@ make-up exam, but failed again!",English); }Punish:^{ NSLog(@"Break Up!"); }]; } 2016-04-21 00:33:31.335 callback[1520:131334] play with my girlfriend! 2016-04-21 00:33:31.350 callback[1520:131334] one month later... 2016-04-21 00:33:31.350 callback[1520:131334] Zhangsan is taking english make-up exam, but failed again! 2016-04-21 00:33:31.350 callback[1520:131334] Break Up!
Block回调执行顺序:ViewDidLoad---张三执行考前玩的方法---打印和女朋友玩,打印一个月后---调用参加补考这个block---执行参加补考这个block,即张三参加英语补考又挂了---调用惩罚这个block---执行惩罚这个block,即分手。
写的不对的地方,希望看到的朋友指正。
时间: 2024-10-10 19:49:18