2012-04-04 10 views
6

czytałem w tym samym miejscu jak wstawka i UILabel (podklasa UILabel i zastąpić wymaganych metod). Przed dodaniem go do mojej aplikacji postanowiłem przetestować go w samodzielnej aplikacji testowej. Kod jest pokazany poniżej.instacji UILabel

Oto MyUILabel.h

#import <UIKit/UIKit.h> 

@interface MyUILabel : UILabel 

@end 

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

Oto mój ViewController.h

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


@interface ViewController : UIViewController 
{ 
    MyUILabel *myDisplay; 
} 

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

@end 

Oto 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 

Żadna z metod MyUILabel.m (które Im nadrzędnym) sprawdzony.

Wgląd w dlaczego jest bardzo doceniany.

Pozdrawiam,

Ramon.

Odpowiedz

5

Ok. Zrobiłem jeszcze kilka kopania, a w Xcode jest widoczne pole patrząc na plik nib. Jest to "Inspektor tożsamości" (trzecia ikona od lewej). To musiało zostać zmienione z UILabel na MyUILabel.

Teraz działa!