自定义NSButtonCell,不调用drawBezelWithFrame [英] Custom NSButtonCell, drawBezelWithFrame not called

查看:109
本文介绍了自定义NSButtonCell,不调用drawBezelWithFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在Cocoa/OSX中自定义绘制按钮.由于我的视图是自定义绘制的,因此我不会使用IB,而是希望在代码中全部完成.我创建了NSButtonCell的子类和NSButton的子类.在NSButtonCell的子类中,我重写方法drawBezelWithFrame:inView:;在我的子类NSButton的initWithFrame方法中,我使用setCell在Button中设置我的CustomCell.但是,drawBezelWithFrame没有被调用,我不明白为什么.有人可以指出我做错了什么或我在这里错过了什么吗?

I'm trying to figure out how to custom draw Buttons in Cocoa/OSX. Since my view is custom drawn I will not use IB and want to do it all in code. I created a subclass of NSButtonCell and a subclass of NSButton. In the Subclass of NSButtonCell I override the Method drawBezelWithFrame:inView: and in the initWithFrame Method of my subclassed NSButton I use setCell to set my CustomCell in the Button. However, drawBezelWithFrame gets not called and I don't understand why. Can someone point out what I've done wrong or what I miss here?

NSButtonCell的子类:

Subclass of NSButtonCell:

#import "TWIButtonCell.h"

@implementation TWIButtonCell

-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
    //// General Declarations
[[NSGraphicsContext currentContext] saveGraphicsState];

    //// Color Declarations
    NSColor* fillColor = [NSColor colorWithCalibratedRed: 0 green: 0.59 blue: 0.886 alpha: 1];

    //// Rectangle Drawing
    NSBezierPath* rectanglePath = [NSBezierPath bezierPathWithRect: NSMakeRect(8.5, 7.5, 85, 25)];
    [fillColor setFill];
    [rectanglePath fill];
    [NSGraphicsContext restoreGraphicsState];
}

@end

NSButton的子类:

Subclass of NSButton:

#import "TWIButton.h"
#import "TWIButtonCell.h"

@implementation TWIButton

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        TWIButtonCell *cell = [[TWIButtonCell alloc]init];
        [self setCell:cell];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

@end

用法:

- (void)addSendButton:(NSRect)btnSendRectRect 
{
    TWIButton *sendButton = [[TWIButton alloc] initWithFrame:btnSendRectRect];
    [self addSubview:sendButton];
    [sendButton setTitle:@"Send"];
    [sendButton setTarget:self];
    [sendButton setAction:@selector(send:)];
}

推荐答案

以下是您的代码中似乎缺少的内容.

Following are the things seems to be missing out from your code.

  1. 您没有调用[super drawRect:dirtyRect]
  2. 您不会覆盖从NSButton派生的Class(TWIButton)中的 +(Class)cellClass .

下面是更改后的代码:

@implementation TWIButton

    - (id)initWithFrame:(NSRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            TWIButtonCell *cell = [[TWIButtonCell alloc]init];
            [self setCell:cell];
        }

        return self;
    }

    - (void)drawRect:(NSRect)dirtyRect
    {
        // Drawing code here.
       //Changes Added!!!
    [super drawRect:dirtyRect];

    }

    //Changes Added!!!!
    + (Class)cellClass
    {
       return [TWIButtonCell class];
    }

    @end

现在将断点保留在drawBezelWithFrame处并检查它是否会被调用.

Now keep the break point at drawBezelWithFrame and check it will get called.

这篇关于自定义NSButtonCell,不调用drawBezelWithFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆