Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 自定义NSButtonCell,未调用带框架的抽框_Macos_Cocoa_Subclass_Nsbutton_Nsbuttoncell - Fatal编程技术网

Macos 自定义NSButtonCell,未调用带框架的抽框

Macos 自定义NSButtonCell,未调用带框架的抽框,macos,cocoa,subclass,nsbutton,nsbuttoncell,Macos,Cocoa,Subclass,Nsbutton,Nsbuttoncell,我想知道如何在Cocoa/OSX中定制按钮。因为我的视图是自定义绘制的,所以我不会使用IB,而是希望在代码中完成这一切。我创建了NSButtonCell的子类和NSButton的子类。在NSButtonCell的子类中,我覆盖了drawBezelWithFrame:inView:方法,在子类NSButton的initWithFrame方法中,我使用setCell在按钮中设置CustomCell。然而,drawBezelWithFrame没有被调用,我不明白为什么。有人能指出我做错了什么或我错过了

我想知道如何在Cocoa/OSX中定制按钮。因为我的视图是自定义绘制的,所以我不会使用IB,而是希望在代码中完成这一切。我创建了NSButtonCell的子类和NSButton的子类。在NSButtonCell的子类中,我覆盖了drawBezelWithFrame:inView:方法,在子类NSButton的initWithFrame方法中,我使用setCell在按钮中设置CustomCell。然而,drawBezelWithFrame没有被调用,我不明白为什么。有人能指出我做错了什么或我错过了什么吗

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的子类:

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

以下是代码中似乎遗漏的内容

您不是在调用[super drawRect:dirtyRect] 您没有覆盖从NSButton派生的ClassTWIButton中的+ClasscellClass。 以下是更改后的代码:

@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,并检查是否会调用它。

以下是代码中似乎遗漏的内容

您不是在调用[super drawRect:dirtyRect] 您没有覆盖从NSButton派生的ClassTWIButton中的+ClasscellClass。 以下是更改后的代码:

@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,并检查是否会调用它。

可以放弃NSButton子类,因为它看起来只用于在初始值设定项中实例化单元格类型。 简单地


顺便说一句,在前面的示例中,您可能会有一个漏洞,因为您先初始化,然后调用setCell,它可能有自己的retain。

您可能会放弃NSButton子类,因为看起来您只是在初始化器中使用它来实例化单元格类型。 简单地


顺便说一句,在前面的示例中,您可能会出现漏洞,因为您先初始化,然后调用setCell,而setCell可能有自己的retain。

谢谢,它的工作方式很有魅力。drawRect方法是由XCode模板创建的,不知道为什么它们没有包含super调用。cellClass在OS X 10.11中已被弃用。你知道现在如何通过避免不推荐的方法来解决这个问题吗?谢谢你,很有魅力。drawRect方法是由XCode模板创建的,不知道为什么它们没有包含super调用。cellClass在OS X 10.11中已被弃用。你知道现在如何通过避免不推荐的方法来解决这个问题吗?