码迷,mamicode.com
首页 > 移动开发 > 详细

IOS Block的回调

时间:2016-04-21 01:24:59      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

//  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,即分手。

写的不对的地方,希望看到的朋友指正。

IOS Block的回调

标签:

原文地址:http://www.cnblogs.com/garywong1949/p/5415147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!