それはUIButtonで@selector引数のためのObjective-Cのブロックを渡すことは可能ですか?
すでに回答がありますが、答えははいですが、いくつかのカテゴリを設定するには少しの作業が必要です。
あなたがオブジェクトとして保存され、呼び出され、タイマーと同様に、このようにして多くのことを行うことができますので、私は等... NSInvocationを使用することをお勧めします...ここで
は私がやったことですが、私はARCを使用しています注意してください。
まずNSObjectの上の簡単なカテゴリである:
の.h
@interface NSObject (CategoryNSObject)
- (void) associateValue:(id)value withKey:(NSString *)aKey;
- (id) associatedValueForKey:(NSString *)aKey;
@end
.M
#import "Categories.h"
#import <objc/runtime.h>
@implementation NSObject (CategoryNSObject)
#pragma mark Associated Methods:
- (void) associateValue:(id)value withKey:(NSString *)aKey {
objc_setAssociatedObject(self, (__bridge void *)aKey, value, OBJC_ASSOCIATION_RETAIN);
}
- (id) associatedValueForKey:(NSString *)aKey {
return objc_getAssociatedObject(self, (__bridge void *)aKey);
}
@end
次のブロックに格納するNSInvocationのカテゴリである:
.h
@interface NSInvocation (CategoryNSInvocation)
+ (NSInvocation *) invocationWithTarget:(id)aTarget block:(void (^)(id target))block;
+ (NSInvocation *) invocationWithSelector:(SEL)aSelector forTarget:(id)aTarget;
+ (NSInvocation *) invocationWithSelector:(SEL)aSelector andObject:(__autoreleasing id)anObject forTarget:(id)aTarget;
@end
。メートル
#import "Categories.h"
typedef void (^BlockInvocationBlock)(id target);
#pragma mark - Private Interface:
@interface BlockInvocation : NSObject
@property (readwrite, nonatomic, copy) BlockInvocationBlock block;
@end
#pragma mark - Invocation Container:
@implementation BlockInvocation
@synthesize block;
- (id) initWithBlock:(BlockInvocationBlock)aBlock {
if ((self = [super init])) {
self.block = aBlock;
} return self;
}
+ (BlockInvocation *) invocationWithBlock:(BlockInvocationBlock)aBlock {
return [[self alloc] initWithBlock:aBlock];
}
- (void) performWithTarget:(id)aTarget {
self.block(aTarget);
}
@end
#pragma mark Implementation:
@implementation NSInvocation (CategoryNSInvocation)
#pragma mark - Class Methods:
+ (NSInvocation *) invocationWithTarget:(id)aTarget block:(void (^)(id target))block {
BlockInvocation *blockInvocation = [BlockInvocation invocationWithBlock:block];
NSInvocation *invocation = [NSInvocation invocationWithSelector:@selector(performWithTarget:) andObject:aTarget forTarget:blockInvocation];
[invocation associateValue:blockInvocation withKey:@"BlockInvocation"];
return invocation;
}
+ (NSInvocation *) invocationWithSelector:(SEL)aSelector forTarget:(id)aTarget {
NSMethodSignature *aSignature = [aTarget methodSignatureForSelector:aSelector];
NSInvocation *aInvocation = [NSInvocation invocationWithMethodSignature:aSignature];
[aInvocation setTarget:aTarget];
[aInvocation setSelector:aSelector];
return aInvocation;
}
+ (NSInvocation *) invocationWithSelector:(SEL)aSelector andObject:(__autoreleasing id)anObject forTarget:(id)aTarget {
NSInvocation *aInvocation = [NSInvocation invocationWithSelector:aSelector
forTarget:aTarget];
[aInvocation setArgument:&anObject atIndex:2];
return aInvocation;
}
@end
は、ここでそれを使用する方法である:
NSInvocation *invocation = [NSInvocation invocationWithTarget:self block:^(id target) {
NSLog(@"TEST");
}];
[invocation invoke];
あなたが起動し、標準のObjective-Cのメソッドで多くのことを行うことができます。たとえば、NSInvocationOperation(initWithInvocation :)、NSTimer(scheduledTimerWithTimeIntervalを使用することができます呼び出し:repeates :)ポイントはNSInvocationにあなたのブロックを回している
が、より汎用性があり、そのように使用することができます。
NSInvocation *invocation = [NSInvocation invocationWithTarget:self block:^(id target) {
NSLog(@"My Block code here");
}];
[button addTarget:invocation
action:@selector(invoke)
forControlEvents:UIControlEventTouchUpInside];
もう一度これは1つの提案に過ぎません。
+1関連オブジェクト – user102008
これは、効率的ではないハッシュ検索を意味する関連オブジェクトを使用するよりも、より効率的な方法でこの問題を解決するために、 'objc_implementationWithBlock()'と 'class_addMethod()メソッドルックアップとして)。無関係なパフォーマンスの違いはありますが、これは代替方法です。 – bbum
@bbumあなたは 'imp_implementationWithBlock'を意味しますか? – vikingosegundo