如何创建我自己的方法,以块作为参数,我可以调用以后? [英] How can I create my own methods which take a block as an argument and which I can call later?

查看:126
本文介绍了如何创建我自己的方法,以块作为参数,我可以调用以后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经尝试过以下的操作。

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



我还试图将块变量传递给我的自定义方法,成功。

更新

p>

这是档案 is.h

  #import< UIKit / UIKit.h> 

typedef void(^ viewCreator)(void);

@interface blocks2ViewController:UIViewController
{

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

这是 .m 档案:

  #importblocks2ViewController.h

@implementation blocks2ViewController
//实现viewDidLoad在加载视图之后执行额外的设置,通常来自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 {
//如果没有超级视图,则释放视图。
[super didReceiveMemoryWarning];

//释放所有未使用的缓存数据,图像等。
}

- (void)viewDidUnload {
//释放主视图的任何保留的子视图。
//例如self.myOutlet = nil;
}

// ...

- (void)createButtonUsingBlocks:(viewCreator)block
{
// viewCreator;
NSLog(@inside creator);
}
@end


解决方案

首先如果要让您的块接受字符串参数, typedef 是关闭的:

  typedef void(^ viewCreator)(NSString *); 

其次的块类型是:

  ReturnType(^)(ParameterTypes ...)


b $ b

而不是

  ReturnType(^ *)(ParameterTypes ...)

因此,不需要向 viewCreator 类型添加指针:

   - (void)createButtonUsingBlocks:(viewCreator) 

第三您实际上必须调用该块

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

第四 UIButton 过度保留 - 您应该发布 autorelease it:

  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


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

解决方案

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

这篇关于如何创建我自己的方法,以块作为参数,我可以调用以后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆