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

Aja*_*dey 10 cocoa-touch block objective-c

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

我试过以下事情.

#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");
}
Run Code Online (Sandbox Code Playgroud)

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


更新

这是文件is.h:

 #import <UIKit/UIKit.h>

typedef void (^viewCreator)(void);

@interface blocks2ViewController : UIViewController
{

}
- (void)createButtonUsingBlocks:(viewCreator)block;
@end
Run Code Online (Sandbox Code Playgroud)

这是.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
Run Code Online (Sandbox Code Playgroud)

Geo*_*che 13

首先typedef处于关闭状态,如果你想让你的模块需要一个字符串参数:

typedef void (^viewCreator)(NSString*);
Run Code Online (Sandbox Code Playgroud)

其次,块的类型是:

ReturnType (^)(ParameterTypes...)
Run Code Online (Sandbox Code Playgroud)

并不是

ReturnType (^*)(ParameterTypes...)
Run Code Online (Sandbox Code Playgroud)

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

- (void)createButtonUsingBlocks:(viewCreator)block;
Run Code Online (Sandbox Code Playgroud)

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

-(void)createButtonUsingBlocks:(viewCreator *)block {
    block(@"button name");
    // ...
Run Code Online (Sandbox Code Playgroud)

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

UIButton *dummyButton = [[UIButton alloc] initWithFrame:...];
// ...    
[self.view addSubview:dummyButton];
[dummyButton release];
Run Code Online (Sandbox Code Playgroud)

扔掉所有这些:

#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
Run Code Online (Sandbox Code Playgroud)

  • 很棒的答案.另外一个说明; 如果您的目标是存储块并稍后调用它,则需要"复制"块.块从堆栈开始,如果在声明堆栈帧被销毁后调用未复制的块,则会发生非常糟糕的事情. (2认同)

Guj*_*min 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");
}
Run Code Online (Sandbox Code Playgroud)