简体   繁体   English

UITextView类别继承无法在iOS7设备上正常工作

[英]UITextView category inheritance fails to work as expected on iOS7 device

I've had working code for categories of UITextField and UITextField similar to the following on iOS5 and iOS6 devices. 我已经为UITextField和UITextField的类别编写了工作代码,类似于在iOS5和iOS6设备上的以下代码。 However, the UITextView category method does not appear to be called on an iOS7 device (an iPhone5S). 但是,似乎未在iOS7设备(iPhone5S)上调用UITextView类别方法。 The code is: 代码是:

@interface UIView (CustomCategory)
-(void) someCategoryMethod;
@end

@implementation UIView (CustomCategory)
-(void) someCategoryMethod {
    NSLog(@"UIView category methods appear to work.");
}
@end

@interface UITextField (CustomCategory)
-(void) someCategoryMethod;
@end

@implementation UITextField (CustomCategory)
-(void) someCategoryMethod {
    [super someCategoryMethod];
    NSLog(@"UITextField category methods appear to work.");
}
@end

@interface UITextView (CustomCategory)
-(void) someCategoryMethod;
@end

@implementation UITextView (CustomCategory)
-(void) someCategoryMethod {
    [super someCategoryMethod];
    NSLog(@"UITextView category methods appear to work.");
}
@end

void testFunction() {
    UITextView* textView = [[[UITextView alloc] init] autorelease];
    [textView someCategoryMethod];

    UITextField* textField = [[[UITextField alloc] init] autorelease];
    [textField someCategoryMethod];
}

On an iOS5 device, this (testFunction) prints: 在iOS5设备上,此(testFunction)打印:

UIView category methods appear to work.
UITextView category methods appear to work.
UIView category methods appear to work.
UITextField category methods appear to work.

However, on an iOS7 device this prints: 但是,在iOS7设备上会打印:

UIView category methods appear to work.
UIView category methods appear to work.
UITextField category methods appear to work.

So the UIView category method is actually being called in preference to the UITextView category method, which appears to contradict this answer . 因此,实际上实际上是优先于UITextView类别方法来调用UIView类别方法,这似乎与该答案相矛盾。

Could anyone clarify whether the above code should work as expected (ie as on iOS5 and iOS6)? 任何人都可以澄清上面的代码是否应该按预期工作(即在iOS5和iOS6上)?

Avoid Category Method Name Clashes 避免类别方法名称冲突

Because the methods declared in a category are added to an existing class, you need to be very careful about method names. 由于在类别中声明的方法已添加到现有类中,因此您需要非常小心方法名称。

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass) , the behavior is undefined as to which method implementation is used at runtime. 如果在一个类中声明的方法的名称是一样的,在原来的类中的方法, 或在同一类其他类别的方法(甚至是超类),以在使用该方法实现的行为是不确定的运行。

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM