NSTextField中的NSButton子视图,为什么我的按钮没有出现? [英] NSButton subview within an NSTextField, why is my button not appearing?

查看:90
本文介绍了NSTextField中的NSButton子视图,为什么我的按钮没有出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在NSTextField的最右边添加一个NSButton图像.具体来说,它将是一个按钮,它将启动NSPopover(图形日期选择器日历).现在,我要做的就是在我的NSTextField的最右边获得图像按钮.

Am trying to add an NSButton image to the far right of an NSTextField. Specifically it is going to be a button which launches an NSPopover (graphical date picker calendar). Right now all I'm trying to do is get the image button on the far right of my NSTextField.

我像这样子类化了NSTextField,这是我的代码:

I have subclassed NSTextField like this and here is my code:

@implementation myDateTextField {

- (void) awakeFromNib {
   // my image button icon is 16px by 16px
   NSRect buttonFrame = NSMakeRect(0.0f,0.0f, 16.0f, 16.0f);
   NSButton *popoverButton = [[NSButton alloc] initWithFrame: buttonFrame];

   popoverButton.buttonType = NSMomentaryChangeButton;
   popoverButton.bezelStyle = NSInlineBezelStyle;
   popoverButton.bordered = NO;
   popoverButton.imagePosition = NSImageOnly;
   [popoverButton setImage:[NSImage imageNamed:@"calendar_button.png"]];
   [popoverButton.cell setHighlightsBy:NSContentsCellMask];

   [self addSubview:popoverButton];
   NSLog(@"awakeFromNib loaded.");
}

}

在Interface Builder中,我将我的NSTextField类设置为myDateTextField,并且可以确认窗口加载时正在调用awakeFromNib.我的问题是按钮根本没有出现!

Within Interface Builder I have set the class my my NSTextField to be myDateTextField and I can confirm that awakeFromNib is being called when my window loads. My problem is the button is not appearing at all!

关于我可能做错了什么的忠告?我意识到该按钮的位置可能会在文本字段中关闭,但现在我要做的就是让它出现.

Any words of advice as to what I may be doing wrong? I realize the location of the button probably will be off within the text field but right now all I'm trying to do is get it to appear.

推荐答案

您的代码中有一个错字(NSRect =,请删除"=).否则,我将运行此代码,并且只要我有一个名为"calendar_button.png"的实际图像都适合该空间,它就可以正常工作.没有图像,代码仍然可以运行,但是什么也没有显示.

There's a typo in your code here (NSRect =, get rid of the "="). Otherwise I ran this code and it worked fine as long as I had an actual image named "calendar_button.png" which fits in the space. Without the image, the code still runs but nothing shows up.

尝试测试是否确实找到了该图像:

Try testing that the image is actually found:

NSImage *calImage = [NSImage imageNamed:@"calendar_button.png"];
if(calImage == nil){
    NSLog(@"Failed to find calendar image!");
}
[popoverButton setImage:calImage];

您也可以尝试将popoverButton.bordered设置为YES来查看按钮的显示位置.

You can also try setting popoverButton.bordered to YES to see where the button will appear.

这篇关于NSTextField中的NSButton子视图,为什么我的按钮没有出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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