objective-c - 在 objective-c 中创建自定义动态类

标签 objective-c ios objective-c-runtime inline-code

在我的应用程序中,我有一个 UIViewController,我使用了很多 UIAlertView 来向用户询问事情。

因为我需要每个 UIAlertView 的响应我已经让我的 Controller 成为 UIAlertViewDelegate 的委托(delegate),这工作正常但是在 7 UIAlertView 之后'我正在努力寻找一种更好的方式来使用委托(delegate)。

在 Java 中,我知道我可以为单一目的创建内联类,就像这个问题:Java - inline class definition

我想知道的是:有没有办法创建一个动态委托(delegate)的类?实现这样的目标

id<UIAlertViewDelegate> myCustomClass = @class {
    my class code goes here
}

UIAlertView* alertView;
alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                       message:@"Message"
                                      delegate:myCustomClass
                             cancelButtonTitle:@"No"
                             otherButtonTitles:@"OK", @"Sure", @"Maybe", nil] ];    
[alertView show];

最佳答案

不——在 Objective-C 中没有“内联类”。话虽如此,您可以在运行时使用 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

关于objective-c - 在 objective-c 中创建自定义动态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12674502/

相关文章:

iphone - 主要在IOS中是如何工作的

ios - CIFilter 减慢滚动速度

ios - 在 RxSwift 中点击按钮不会在平面 map 范围内调用 Moya 网络请求

objective-c - 在 ARC 下,使用运行时方法分配给对象类型的 ivar 是否合法/安全?

ios - 有什么方法可以列出 iOS 应用程序中的所有 swizzled 方法吗?

iphone - Zbar SDK 和 ios7/xcode 5 - 应用程序的 CPU 使用率达到 100%,内存超过 100MB

ios - NSTimer 不调用他的选择器

ios - 我可以在不将神秘的闭源库包含到我的应用程序中的情况下获得 iOS 分析吗?

ios - 动画化多个类似uiscrollviews的动画

ios - 使用可变参数调配方法并转发消息 - Bad Access