First the typedef
is off if you want to let your blocks take a string parameter:
typedef void (^viewCreator)(NSString*);
Secondly the type for blocks is:
ReturnType (^)(ParameterTypes...)
and not
ReturnType (^*)(ParameterTypes...)
Thus there is no need to add pointers to the viewCreator
type:
- (void)createButtonUsingBlocks:(viewCreator)block;
Third you actually have to call the block if you are not doing that yet:
-(void)createButtonUsingBlocks:(viewCreator *)block {
block(@"button name");
// ...
Fourth and last, the UIButton
is over-retained - you should release
or autorelease
it:
UIButton *dummyButton = [[UIButton alloc] initWithFrame:...];
// ...
[self.view addSubview:dummyButton];
[dummyButton release];
Throwing all that together:
#import <UIKit/UIKit.h>
typedef void (^viewCreator)(NSString*);
@interface blocks2ViewController : UIViewController {}
-(void)createButtonUsingBlocks:(viewCreator)block;
@end
@implementation blocks2ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createButtonUsingBlocks:^(NSString *name) {
UIButton *dummyButton =
[[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];
dummyButton.backgroundColor = [UIColor greenColor];
[self.view addSubview:dummyButton];
[dummyButton release];
}];
}
-(void)createButtonUsingBlocks:(viewCreator)block {
block(@"my button name");
}
@end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…