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

编程入门 行业动态 更新时间:2024-10-23 01:58:18
本文介绍了如何创建我自己的方法,以块作为参数,我可以调用以后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

#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

更多推荐

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

本文发布于:2023-11-25 02:16:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自己的   参数   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!