iOS開發 - runtime運行時一些實用方法

一、從別的類拷貝方法get

//說明:StyleKit是類名 該方法尋找類中包含draw的方法string

- (void)searchMethodsit

{io

    self.methodsArray = [@[] mutableCopy];table

    int unsigned numMethods;class

    Method *methods = class_copyMethodList(objc_getMetaClass("StyleKit"), &numMethods);List

    for (int i = 0; i < numMethods; i++) {方法

        NSString *stringFromSelector = NSStringFromSelector(method_getName(methods[i]));im

        if ([stringFromSelector rangeOfString:@"draw"].location != NSNotFound) {tab

            [self.methodsArray addObject:stringFromSelector];

        }

    }

    free(methods);

}

 

//說明:self.imageList 整數取0便可 將StyleKit類中搜出來的方法,經過IMP調用執行

- (void)drawRect:(CGRect)rect

{

    if (self.imageList < self.methodsArray.count) {

        SEL paintCodeSelector = NSSelectorFromString(self.methodsArray[self.imageList]);

        [StyleKit setDesiredFillColor:self.fillColor];

        IMP imp = [[StyleKit class] methodForSelector:paintCodeSelector];

        void (*func)(id, SEL) = (void *)imp;

        func([StyleKit class], paintCodeSelector);

    }

}

相關文章
相關標籤/搜索