6

この同じサイトで、挿入およびUILabelの方法を読みました(UILabelをサブクラス化し、必要なメソッドをオーバーライドします)。アプリに追加する前に、スタンドアロンのテスト アプリでテストすることにしました。コードを以下に示します。

ここに MyUILabel.h があります

#import <UIKit/UIKit.h>

@interface MyUILabel : UILabel

@end

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

ここに私のViewController.hがあります

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


@interface ViewController : UIViewController
{
    MyUILabel   *myDisplay;
}

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

@end

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

MyUILabel.m のメソッド (Im がオーバーライドしている) は呼び出されません。

その理由についての洞察は大歓迎です。

よろしく、

ラモン。

4

1 に答える 1

5

Ok。さらに掘り下げたところ、Xcode では、nib ファイルを見るとフィールドが表示されます。「Identity Inspector」(左から 3 番目のアイコン)です。これを UILabel から MyUILabel に変更する必要がありました。

今それは動作します!

于 2012-04-05T06:15:15.460 に答える