كيف يمكنني إنشاء أساليب خاصة بي التي تأخذ كتلة كوسيطة وأيها يمكنني الاتصال بها لاحقًا؟
-
01-10-2019 - |
سؤال
كيف يمكنني إنشاء أساليب خاصة بي التي تأخذ كتلة كوسيطة وأيها يمكنني الاتصال بها لاحقًا؟
لقد حاولت اتباع الأشياء.
#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
المحلول
أولاً ال typedef
يتم إيقاف تشغيله إذا كنت تريد السماح لكتلك بأخذ معلمة سلسلة:
typedef void (^viewCreator)(NSString*);
ثانيًا نوع الكتل هو:
ReturnType (^)(ParameterTypes...)
و لا
ReturnType (^*)(ParameterTypes...)
وبالتالي ليست هناك حاجة لإضافة مؤشرات إلى viewCreator
يكتب:
- (void)createButtonUsingBlocks:(viewCreator)block;
ثالث عليك بالفعل الاتصال بالكتلة إذا كنت لا تفعل ذلك بعد:
-(void)createButtonUsingBlocks:(viewCreator *)block {
block(@"button name");
// ...
الرابع وأخيرا ، 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
نصائح أخرى
يمكنك أيضًا القيام بذلك بهذه الطريقة دون تحديد الطريقة المسبقة:
@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");
}
لا تنتمي إلى StackOverflow