SubClassing UILabel

I read on the same site how to insert UILabel (subclass UILabel and override the necessary methods). Before adding it to my application, I decided to test it in a standalone test application. The code is shown below.

Here is MyUILabel.h

#import <UIKit/UIKit.h> @interface MyUILabel : UILabel @end 

Here is MyUILabel.m

 #import "MyUILabel.h" #import <QuartzCore/QuartzCore.h> @implementation MyUILabel - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } // for border and rounding -(void) drawRect:(CGRect)rect { self.layer.cornerRadius = 4.0; self.layer.borderWidth = 2; [super drawRect:rect]; } // for inset -(void) drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect: UIEdgeInsetsInsetRect(rect, insets)]; } 

Here is my ViewController.h

 #import <UIKit/UIKit.h> #import "MyUILabel.h" @interface ViewController : UIViewController { MyUILabel *myDisplay; } @property (strong, nonatomic) IBOutlet MyUILabel *myDisplay; @end 

Here is ViewController.m:

 #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize myDisplay; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. myDisplay.text = @"Hello World!"; } - (void)viewDidUnload { [self setMyDisplay:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end 

None of the methods in MyUILabel.m (which overrides Im) are called.

Understanding why we value so much.

Hi,

Ramon

+6
source share
1 answer

Ok I did another digging, and in Xcode there is a field visible when viewing the nib file. His "Identity Inspector" (third icon on the left). This had to be changed from UILabel to MyUILabel.

Now it works!

+5
source

Source: https://habr.com/ru/post/912355/


All Articles