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;

3 番目に、まだ行っていない場合は、実際にブロックを呼び出す必要があります。

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

4つ目と最後に、UIButtonが過剰に保持されています。次のようにする必要がありreleaseますautorelease

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 に答える