0

NSTextFieldがクリックされたときにNSComboBoxを非表示にするにはどうすればよいですか?これは私が使用しているコードです:

クラスcomboBox:(インターフェイスビルダーでNSComboBoxのカスタムクラスとして使用されます)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

クラスtxtField:(インターフェイスビルダーでNSTextFieldのカスタムクラスとして使用されます)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

ただし、機能しません。TextFieldをクリックしても、何も起こりません。アドバイスありがとうございます。

4

2 に答える 2

0

デリゲートメソッドを使用できますNSTextfield

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

アップデート

Appleは、NSTrackingAreasのドキュメントと例を提供しています。

- (void) viewWillMoveToWindow:(NSWindow *)newWindow {
    // Setup a new tracking area when the view is added to the window.
    NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[textfield frame] options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void) mouseEntered:(NSEvent*)theEvent {
    // Mouse entered tracking area.
}

- (void) mouseExited:(NSEvent*)theEvent {
    // Mouse exited tracking area.
}
于 2013-03-06T14:57:23.310 に答える
0

あなたのmouseDown:方法がここの犯人です。NIBでcomboBox1を参照する代わりに、毎回comboBox1の新しいインスタンスを作成し、その新しいインスタンスに「非表示」にするように指示します。そこでメモリリークが発生すると、NSTextFieldをクリックするたびに新しいcomboBox1が必要になることはおそらくないでしょう。

代わりに、NSTextFieldのデリゲートメソッドを使用して、必要なものを取得してください。

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

IBを使用しているので、txtField1とcomboBox1の両方を備えたView-またはWindowControllerがあると思います。ViewController(またはWindowController)で、ViewControllerをNSTextFieldのデリゲートとして設定し、comboBox1にデリゲートメソッドの1つで非表示にするように指示します。

例:

ViewController.hで、最初に両方のオブジェクトを宣言します。

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

次に、実装では:

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

アウトレットをInterfaceBuilderのViewControllerに接続することを忘れないでください。またdelegate、txtField1のアウトレットをViewcontrollerに接続します。

于 2013-03-06T15:11:49.930 に答える