How can I create my own methods which take a block as an argument and which I can call later?

StackOverflow https://stackoverflow.com/questions/3674490

  •  01-10-2019
  •  | 
  •  

Pergunta

How can I create my own methods which take a block as an argument and which I can call later?

I have tried following things.

#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");
}

I have also tried to pass the block variable to my custom method but without any success. Why it is so and what is the right way to do this?


Update

This is file is.h:

 #import <UIKit/UIKit.h>

typedef void (^viewCreator)(void);

@interface blocks2ViewController : UIViewController
{

}
- (void)createButtonUsingBlocks:(viewCreator)block;
@end

And this is the .m file:

#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
Foi útil?

Solução

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

Outras dicas

You can also do it this way without pre-defining the method:

@interface blocks2ViewController : UIViewController
-(void)createButtonUsingBlocks:(void (^)(NSString *name))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:(void (^)(NSString *name))block
{
    block(@"My Name Here");
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top