2010-09-09 8 views
9

Làm thế nào tôi có thể tạo các phương pháp của riêng mình để lấy một khối làm đối số và tôi có thể gọi nó sau?Làm cách nào tôi có thể tạo các phương thức của riêng mình để lấy một khối làm đối số và tôi có thể gọi nó sau?

Tôi đã thử những điều sau đây.

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

Tôi cũng đã cố chuyển biến khối vào phương thức tùy chỉnh của mình nhưng không thành công. Tại sao nó là như vậy và cách đúng để làm điều này là gì?


Cập nhật

Đây là tập tin is.h:

#import <UIKit/UIKit.h> 

typedef void (^viewCreator)(void); 

@interface blocks2ViewController : UIViewController 
{ 

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

Và đây là .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 
+0

Hãy * thêm cập nhật * thay vì thay đổi câu hỏi ban đầu và kiểm tra định dạng trong chế độ xem trước * trước * gửi bài. –

+0

Bản cập nhật vẫn có vấn đề với typedef - thay đổi 'typedef void (^ viewCreator) (void);' to 'typedef void (^ viewCreator) (NSString *);' –

Trả lời

12

Đầu tiên các typedef tắt nếu bạn muốn cho các khối của bạn mất một tham số chuỗi:

typedef void (^viewCreator)(NSString*); 

Thứ hai loại cho các khối là:

ReturnType (^)(ParameterTypes...) 

và không

ReturnType (^*)(ParameterTypes...) 

Như vậy có không cần thêm con trỏ vào loại viewCreator:

- (void)createButtonUsingBlocks:(viewCreator)block; 

Thứ ba bạn thực sự phải gọi khối nếu bạn không làm điều đó được nêu ra:

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

Thứ tư và cuối cùng, các UIButton là quá giữ lại - bạn nên release hoặc autorelease nó:

UIButton *dummyButton = [[UIButton alloc] initWithFrame:...]; 
// ...  
[self.view addSubview:dummyButton]; 
[dummyButton release]; 

Ném tất cả cùng nhau:

#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 
+0

Không có nó vẫn không hoạt động cho tôi .... –

+0

@Ajay: Xem chỉnh sửa, bạn có thực sự đang gọi khối ở đâu đó không? –

+2

Câu trả lời thú vị. Một lưu ý bổ sung; nếu mục tiêu của bạn là để lưu trữ các khối và gọi nó sau này, bạn cần phải 'copy' khối.Các khối bắt đầu trên ngăn xếp và những điều rất xấu sẽ xảy ra nếu gọi một khối không được sao chép sau khi khung ngăn xếp khai báo đã bị hủy. – bbum

6

Bạn cũng có thể làm theo cách này mà không cần tiền xác định phương pháp:

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