问题
I created a subclass of UITextField to override the method rightViewRectForBounds.
Here is my custom class CustomTextField
@interface CustomTextField : UITextField
@end
@implementation CustomTextField
// override rightViewRectForBounds method:
- (CGRect)rightViewRectForBounds:(CGRect)bounds{
NSLog(@"rightViewRectForBounds");
CGRect rightBounds = CGRectMake(bounds.origin.x + 10, 0, 30, 44);
return rightBounds ;
}
@end
Now I set up my ViewController to call the custom class instead of UITextField.
#import "OutputViewController.h"
#import "CustomTextField.h"
@interface OutputViewController () <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet CustomTextField *field1;
@property (strong, nonatomic) IBOutlet UILabel *label1;
- (void)methodName
{
self.field1.rightView = self.label1;
}
The property rightView should call my method override according to Apple's Documentation: "The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver". Why isn't my override working?
Sorry if this is a bad question. I've only been programming for a month.
回答1:
The problem is most likely that field1 is not in fact a CustomTextField. It is easy to confirm this with a breakpoint or some logging.
Remember, it is not enough to declare the class of something. That thing must actually be that class (polymorphism). An instance has a class, quite without regard for how you may cast or declare a variable that refers to it.
来源:https://stackoverflow.com/questions/22748450/subclass-override-method-not-being-called