11

如何创建自己的方法,将块作为参数,以后可以调用?

我已经尝试过以下事情。

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

我也尝试将块变量传递给我的自定义方法,但没有任何成功。为什么会这样,正确的方法是什么?


更新

这是文件is.h

 #import <UIKit/UIKit.h>

typedef void (^viewCreator)(void);

@interface blocks2ViewController : UIViewController
{

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

这是.m文件:

#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
4

2 回答 2

13

typedef如果您想让您的块采用字符串参数,则首先关闭:

typedef void (^viewCreator)(NSString*);

其次,块的类型是:

ReturnType (^)(ParameterTypes...)

并不是

ReturnType (^*)(ParameterTypes...)

因此无需添加指向该viewCreator类型的指针:

- (void)createButtonUsingBlocks:(viewCreator)block;

第三,如果您还没有这样做,您实际上必须调用该块:

-(void)createButtonUsingBlocks:(viewCreator *)block {
    block(@"button name");
    // ...

第四也是最后UIButton一点,过度保留 - 你应该releaseautorelease它:

UIButton *dummyButton = [[UIButton alloc] initWithFrame:...];
// ...    
[self.view addSubview:dummyButton];
[dummyButton release];

所有这些放在一起:

#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
于 2010-09-09T07:26:34.030 回答
6

您也可以在不预先定义方法的情况下这样做:

@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");
}
于 2012-01-11T19:23:02.200 回答