首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SubClassing UILabel

SubClassing UILabel
EN

Stack Overflow用户
提问于 2012-04-04 03:16:41
回答 1查看 3.4K关注 0票数 6

我在同一个站点中阅读了如何嵌入和UILabel (子类UILabel和重写所需的方法)。在将它添加到我的应用程序之前,我决定在一个独立的测试应用程序中测试它。代码如下所示。

这里是MyUILabel.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface MyUILabel : UILabel

@end

这里是MyUILabel.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import "MyUILabel.h"


@interface ViewController : UIViewController
{
    MyUILabel   *myDisplay;
}

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

@end

这里是ViewController.m:

代码语言:javascript
复制
#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 (我重写的)中的任何方法都不会被调用。

对为什么会受到极大赞赏的洞察力。

致以敬意,

雷蒙。

EN

回答 1

Stack Overflow用户

发布于 2012-04-05 06:15:15

好的。我做了一些进一步的研究,在Xcode中有一个字段在查看nib文件时是可见的。它是“身份检查器”(左边是第三个图标)。这需要从UILabel更改为MyUILabel。

现在起作用了!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10004466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档