2012-04-04 12 views
6

Ho letto in questo stesso sito come inserire e UILabel (sottoclasse UILabel e sovrascrive i metodi richiesti). Prima di aggiungerlo alla mia app ho deciso di provarlo in un'app di prova indipendente. Il codice è mostrato sotto.Sottoclasse UILabel

Ecco MyUILabel.h

#import <UIKit/UIKit.h> 

@interface MyUILabel : UILabel 

@end 

Ecco 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)]; 
} 

Ecco il mio ViewController.h

#import <UIKit/UIKit.h> 
#import "MyUILabel.h" 


@interface ViewController : UIViewController 
{ 
    MyUILabel *myDisplay; 
} 

@property (strong, nonatomic) IBOutlet MyUILabel *myDisplay; 

@end 

Ecco 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 

Nessuno dei metodi in MyUILabel.m (che Im predominante) vengono chiamati.

Approfondimenti sul perché sono molto apprezzati.

Saluti,

Ramon.

risposta

5

Ok. Ho fatto qualche altro scavo e in Xcode c'è un campo visibile guardando il file del pennino. È 'Identity Inspector' (terza icona da sinistra). Questo doveva essere cambiato da UILabel a MyUILabel.

Ora funziona!