OC中回调函数得使用(block)

main.m
//
//  main.m
//  Block1
//
//  Created by Rayln Guan on 8/30/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Button.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Button *btn = [[Button alloc] init];
        btn.block = ^(Button *btn){
            NSLog(@"Button 被点击了");
        };
        
        [btn click];
        [btn release];
    }
    return 0;
}



Button.h
//
//  Button.h
//  Block1
//
//  Created by Rayln Guan on 8/30/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import <Foundation/Foundation.h>

@class Button;

typedef void (^ButtonBlock) (Button *);

@interface Button : NSObject

@property ButtonBlock block;

- (void) click;

@end


Button.m
//
//  Button.m
//  Block1
//
//  Created by Rayln Guan on 8/30/13.
//  Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import "Button.h"

@implementation Button

- (void)click{
    _block(self);
}

@end

猜你喜欢

转载自rayln.iteye.com/blog/1933879
今日推荐