2012-10-01 5 views
6

私のアプリケーションでは、私はUIAlertViewをたくさん使ってユーザーに物事を尋ねるのにUIViewControllerを持っています。私は私のコントローラUIAlertViewDelegateのデリゲート作られている各UIAlertViewの応答を必要とするのでobjective-cでカスタムダイナミッククラスを作成する

が、これは正常に動作しますが、7 UIAlertViewのi`mは、デリゲートを使用するためのより良い方法を見つけることを試みた後。私はこの質問のように、単一の目的のためにインラインクラスを作成することができます知っているJavaで

Java - inline class definition

私が知りたいのは何がある:動的委任するクラスを作成する方法はありますか?このようなことを達成するには

+0

'であなたが合格ブロックを呼び出すために、独自のデリゲートを教えてくれること、(関連オブジェクトを使用している場合またはカテゴリ)あなたはサブクラスを作成できることですしていますあなたが思っているものとは全く違うObj-Cの意味です。それは、種類の「フォワーディングクラス」、FYIを宣言します。 –

+1

'' @ class''を使用するポイントは、私が何を考えているのかを誰かに説明するのを助けるために、同様のものを表示することです。ちょうど –

答えて

14

いいえ - Objective-Cには「インラインクラス」はありません。これで、目的に合ったオブジェクトを実行時に作成することができますが、これは少し複雑ですが、私はあなたが言っていることをするためにいくつかのコードを共有したいと思います。ここ

がその一例である:

NSObjectの+ Subclass.h

#import <objc/runtime.h> 

typedef struct selBlockPair { SEL aSEL; id (^__unsafe_unretained aBlock)(id, ...); } selBlockPair; 
#define NIL_PAIR ((struct selBlockPair) { 0, 0 }) 
#define PAIR_LIST (struct selBlockPair []) 
#define BLOCK_CAST (id (^)(id, ...)) 

@interface NSObject (subclass) 

+(Class) newSubclassNamed:(NSString *) name 
      protocols:(Protocol **) protos 
       impls:(selBlockPair *) impls; 

@end 

NSObjectの+ Subclass.m

@implementation NSObject (subclass) 

+(Class) newSubclassNamed:(NSString *)name 
      protocols:(Protocol **)protos 
       impls:(selBlockPair *)impls 
{ 
    if (name == nil) 
    { 
     // basically create a random name 
     name = [NSString stringWithFormat:@"%s_%i_%i", class_getName(self), arc4random(), arc4random()]; 
    } 

    // allocated a new class as a subclass of self (so I could use this on a NSArray if I wanted) 
    Class newClass = objc_allocateClassPair(self, [name UTF8String], 0); 

    // add all of the protocols untill we hit null 
    while (protos && *protos != NULL) 
    { 
     class_addProtocol(newClass, *protos); 
     protos++; 
    } 

    // add all the impls till we hit null 
    while (impls && impls->aSEL) 
    { 
     class_addMethod(newClass, impls->aSEL, imp_implementationWithBlock(impls->aBlock), "@@:*"); 
     impls++; 
    } 

    // register our class pair 
    objc_registerClassPair(newClass); 

    return newClass; 
} 

@end 

使用例:

int main() 
{ 
    @autoreleasepool { 
     __strong Class newClass = [NSString newSubclassNamed:@"MyCustomString" protocols:NULL impls: PAIR_LIST { 
      @selector(description), 
      BLOCK_CAST ^id (id self) { 
       return @"testing"; 
      }, 
      NIL_PAIR 
     }]; 

     NSString *someString = [newClass new]; 
     NSLog(@"%@", someString); 
    } 
} 

出力:

 
2012-10-01 10:07:33.609 TestProj[54428:303] testing 
+0

あなたの回答はインラインクラスに関するすべての質問に回答し、あなたのコードに関するもう1つの質問を開きました。本当にありがとう。今あなたが投稿したこのコードを理解しようとしています。再びありがとう –

+0

@ニコスカラリス問題は理解している場合は、質問するのを恐れることはありません! –

+0

ブリリアント!簡単な質問ですが、カスタムimplで '[super description]'を呼び出す簡単な方法はありますか? – ccwasden

3

Javaの匿名インナークラスのこのタイプは、Objective-Cでサポートされているものではありません。デリゲートに個別に回答したい場合は、ブロックを試してみるのがよいでしょう。

アップルはUIAlertViewsにブロックを追加していませんが、あなた自身で実装することができます。多くの人がこれをオンラインで行っています。ここをクリックしてください:http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet/またはhttps://github.com/MugunthKumar/UIKitCategoryAdditions

基本的な考え方は、独自のデリゲートこととclass` @

+0

あなたの答えは私もたくさん助けます、タンク –

関連する問題