Yog*_*ton 2 objective-c uikit ios method-swizzling
我希望能够像textFieldDidBeginEditing:
和那样使用Swizzle协议方法tableView:didSelectRowAtIndexPath:
.如何才能做到这一点?
首先:你需要很好的理由来调整方法.
没有"方法swizzing协议".方法调配意味着您为选择器交换方法实现指针.协议没有实现.
您可以调整协议中声明的方法.但是你在符合协议的类中完成,而不是在协议本身.
以下是查找实现方法的类的示例:
SEL selector = …; // The selector for the method you want to swizzle
IMP code = …; // The implementation you want to set
Protocol *protocol = …; // The protocol containing the method
// Get the class list
int classesCount = objc_getClassList ( NULL, 0 );
Class *classes = malloc( classCount * sizeof(Class));
objc_getClassList( classes, classesCount );
// For every class
for( int classIndex = 0; classIndex < classesCount; classIndex++ )
{
Class class = classes[classIndex];
// Check, whether the class implements the protocol
// The protocol confirmation can be found in a super class
Class conformingClass = class;
while(conformingClass!=Nil)
{
if (class_conformsToProtocol( conformingClass, protocol )
{
break;
}
conformingClass = class_getSuperclass( conformingClass );
}
// Check, whether the protocol is found in the class or a superclass
if (conformingClass != Nil )
{
// Check, whether the protocol's method is implemented in the class,
// but NOT the superclass, because you have to swizzle it there. Otherwise
// it would be swizzled more than one time.
unsigned int methodsCount;
Method *methods = class_copyMethodList( class, &methodsCount );
for( unsigned methodIndex; methodIndex < methodsCount; methodIndex++ )
{
if (selector == method_getName( methods[methodIndex] ))
{
// Do the method swizzling
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1551 次 |
最近记录: |