今天,我看到了UIView的类别重新加载respontsToSelector和forwardInvocation的代码。我不明白为什么这样做。此代码旨在统一iOS6和iOS7中的UITableview样式。我知道resolveInstanceMethod和forwardInvocation可以在运行时更改功能。 如何解释respondsToSelector和forwardInvocation。当ios调用respondsToSelector?
我做了一个简单的测试,但它没有调用respondsToSelector,测试代码如下:
@protocol MyViewDelegate <NSObject>//why cannot be <UIView>?
@optional
- (void)myViewDelegateMethod;
@end
@interface MyView : UIView
@property (nonatomic,weak) id<MyViewDelegate>delegate;
- (void)test;
@end
@implementation MyView
- (void)test{
NSLog(@"%s",__func__);
if ([self.delegate respondsToSelector:@selector(myViewDelegateMethod)]) {
[self.delegate myViewDelegateMethod];
}
}
@end
@implementation UIView (FunctionChain)
- (BOOL)respondsToSelector:(SEL)aSelector{
//when i call [myview test],can't execute this
return [super respondsToSelector:aSelector];
}
@end