¿Cómo puedo crear mis propios métodos que toman un bloque como argumento y que puedo llamar más tarde?
¿Cómo puedo crear mis propios métodos que toman un bloque como argumento y que puedo llamar más tarde?
He intentado seguir las cosas.
#import <UIKit/UIKit.h>
typedef void (^viewCreator)(void);
@interface blocks2ViewController : UIViewController
{
}
-(void)createButtonUsingBlocks:(viewCreator *)block;
@end
- (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];
}];
}
-(void)createButtonUsingBlocks:(viewCreator *)block
{
// Do something
NSLog(@"inside creator");
}
También intenté pasar la variable de bloque a mi método personalizado pero sin ningún éxito. ¿Por qué es así y cuál es la forma correcta de hacer esto?
Actualizar
Este es el archivois.h
:
#import <UIKit/UIKit.h>
typedef void (^viewCreator)(void);
@interface blocks2ViewController : UIViewController
{
}
- (void)createButtonUsingBlocks:(viewCreator)block;
@end
Y este es el.m
archivo:
#import "blocks2ViewController.h"
@implementation blocks2ViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (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)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// ...
-(void)createButtonUsingBlocks:(viewCreator)block
{
// viewCreator;
NSLog(@"inside creator");
}
@end