gpt4 book ai didi

objective-c - 我如何创建自己的方法,将 block 作为参数并稍后调用?

转载 作者:太空狗 更新时间:2023-10-30 03:25:45 26 4
gpt4 key购买 nike

我如何创建自己的方法,将 block 作为参数并稍后调用?

我已经尝试过以下事情。

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

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


更新

这是文件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

最佳答案

首先如果你想让你的 block 接受一个字符串参数,那么typedef是关闭的:

typedef void (^viewCreator)(NSString*);

其次 block 的类型是:

ReturnType (^)(ParameterTypes...)

而不是

ReturnType (^*)(ParameterTypes...)

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

- (void)createButtonUsingBlocks:(viewCreator)block;

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

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

第四个也是最后一个,UIButton 被过度保留 - 您应该释放自动释放它:

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

关于objective-c - 我如何创建自己的方法,将 block 作为参数并稍后调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com