使NSComboBox在NSTextField被点击时出现 [英] Make NSComboBox Appear when NSTextField is clicked

查看:588
本文介绍了使NSComboBox在NSTextField被点击时出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单击NSTextField时使NSComboBox消失?
这是我使用的代码:



类comboBox:(在接口生成器中用作我的NSComboBox的自定义类)
comboBox。 h:

  #import  
@interface comboBox1:NSComboBox
- (void)隐藏;
@end

comboBox.m:


$ b b

  #importcomboBox1.h
@implementation comboBox1
- (void)隐藏
{
[self setHidden:YES ];
}
@end

类txtField :(用作我的自定义类接口构建器中的NSTextField)
txtField.h:

  #import< Cocoa / Cocoa.h> 
@interface txtField1:NSTextField
@end

txtField.m: p>

  #importtxtField1.h
#importcomboBox1.h
@implementation txtField1
- (void)mouseDown:(NSEvent *)theEvent
{
comboBox1 * obj = [[comboBox1 alloc] init];
[obj Hide];
}
@end

但它不工作: TextField什么也没有发生。 c> mouseDown: 方法是这里的罪魁祸首。而不是引用到你的NIB中的comboBox1,你每次都创建一个新的comboBox1实例,并告诉新的实例隐藏。在泄漏的内存旁边,你可能不想要一个新的comboBox1每次单击NSTextField。



而是使用NSTextField的委托方法来获得所需的内容。

  - (void)controlTextDidBeginEditing:(NSNotification *)obj; 
- (void)controlTextDidEndEditing:(NSNotification *)obj;
- (void)controlTextDidChange:(NSNotification *)obj;

因为你使用IB,我假设你有一个View-或WindowController txtField1和comboBox1。
在你的ViewController(或WindowController)中设置ViewController作为NSTextField的委托,并告诉comboBox1隐藏在一个委托方法中。



/ p>

在您的ViewController.h中首先声明两个对象:

  @property (assign)IBOutlet comboBox1 * comboBox1; 
@property(assign)IBOutlet txtField1 * txtField1;

然后在您的实现中:

   - (void)controlTextDidBeginEditing:(NSNotification *)obj {
[comboBox1 hide];
}

只要不要忘记在Interface Builder中将插座连接到ViewController。还要将txtField1的委托插座连接到ViewController。


How can I make an NSComboBox disappear when an NSTextField is clicked? This is the code I'm using:

Class comboBox: (used as custom class for my NSComboBox in the interface builder) comboBox.h:

#import <Cocoa/Cocoa.h>
@interface comboBox1 : NSComboBox
-(void)Hide;
@end

comboBox.m:

#import "comboBox1.h"
@implementation comboBox1
-(void)Hide
{
    [self setHidden:YES];
}
@end

Class txtField: (used as custom class for my NSTextField in the interface builder) txtField.h:

#import <Cocoa/Cocoa.h>
@interface txtField1 : NSTextField
@end

txtField.m:

#import "txtField1.h"
#import "comboBox1.h"
@implementation txtField1
-(void)mouseDown:(NSEvent *)theEvent
{
    comboBox1 *obj = [[comboBox1 alloc] init];
    [obj Hide];
}
@end

But it doesn't work: when click the TextField nothing happens. Thank you in advice.

解决方案

Your mouseDown: method is the culprit here. Instead of referencing to the comboBox1 in your NIB, you create a new instance of comboBox1 every time and tell that new instance to 'hide'. Next to leaking memory there, you probably don't want a new comboBox1 every time you click the NSTextField.

Instead use NSTextField's delegate methods to get what you want.

- (void)controlTextDidBeginEditing:(NSNotification *)obj;
- (void)controlTextDidEndEditing:(NSNotification *)obj;
- (void)controlTextDidChange:(NSNotification *)obj;

Since you're using IB I assume you got a View- or WindowController with both the txtField1 and the comboBox1. In your ViewController (or WindowController) set the ViewController as the NSTextField's delegate and tell the comboBox1 to hide in one of the delegate methods.

An example:

In your ViewController.h first declare both objects:

@property (assign) IBOutlet comboBox1 *comboBox1;
@property (assign) IBOutlet txtField1 *txtField1;

Then in your implementation:

- (void)controlTextDidBeginEditing:(NSNotification *)obj {
    [comboBox1 hide];
}

Just don't forget to connect the outlets to your ViewController in Interface Builder. Also connect the delegate outlet of the txtField1 to your Viewcontroller.

这篇关于使NSComboBox在NSTextField被点击时出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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