简体   繁体   English

子类化NSTextField在用户输入后生成内存警告

[英]Subclassed NSTextField generates memory warnings after user input

I have implemented a subclassed version of NSTextField , which I've called CustomTextField , the code for which is below: 我实现了NSTextField的子类版本,我将其称为CustomTextField ,其代码如下:

@interface CustomTextField : NSTextField

@property (nonatomic, strong) IBInspectable NSImage *backgroundImage;

@end

@implementation CustomTextField

- (void)awakeFromNib
{
    [self setDrawsBackground:NO];

}

- (void)drawRect:(NSRect)rect
{
    NSImage *backgroundImage = self.backgroundImage;

    [backgroundImage drawInRect:rect fromRect:rect operation:NSCompositeSourceOver fraction:1.0];

    [super drawRect:rect];
}

@end

I have three instances of this custom text field, which I've set-up in my XIB file. 我在XIB文件中设置了这个自定义文本字段的三个实例。 When I run the app, select a text field, type in some text, and hit 'Enter', I get the following output from Xcode: 当我运行该应用程序时,选择一个文本字段,输入一些文本,然后单击“ Enter”,我将从Xcode获得以下输出:

malloc: protecting edges
malloc: enabling scribbling to detect mods to free blocks
malloc: nano zone does not support guard pages
malloc: purgeable zone does not support guard pages

My guess is that my subclass implementation is not handling something correctly, but I'm honestly not sure. 我的猜测是我的子类实现无法正确处理某些事情,但老实说我不确定。 Does anyone have some suggestions? 有人有建议吗? Thanks! 谢谢!

You should call 你应该打电话

[super awakeFromNib];

in your awakeFromNib method. 在您的awakeFromNib方法中。

From the docs : 文档

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. 您必须调用awakeFromNib的超级实现,以使父类有机会执行其所需的任何其他初始化。 Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. 尽管此方法的默认实现不执行任何操作,但是许多UIKit类提供了非空实现。 You may call the super implementation at any point during your own awakeFromNib method. 您可以在自己的awakeFromNib方法中的任何时候调用超级实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM