XCode 4.6 でアプリケーションを開発しています。
NSTextField コントロールからテキスト変更通知を取得するには、次のようにします。
- NSTextField コントロールをウィンドウに配置します。
- IB で右クリックしてコントロール デリゲートをファイルの所有者に接続し、デリゲートからファイルの所有者にドラッグします。
- window クラスに controlTextDidChange を実装します。
アプリケーションの場合、ウィンドウ クラスは AppDelegate で、ファイルの所有者は NSApplication です。モーダル ダイアログの場合、ウィンドウ クラス、NSWindowController およびファイルの所有者は同じタイプです。
appDelegate クラスの controlTextDidChange にブレークポイントを設定すると、ブレークポイントは起動しません。モーダル ダイアログで同じ手順を実行すると、正常に動作します。
メイン アプリケーション ウィンドウのケースでは、コントロールのデリゲートが AppDelegate ではないことがわかっています。
コントロール デリゲートをメイン ウィンドウに接続する際に何が間違っていますか? 私は単純なものが欠けているに違いありません。ファイルの所有者は、コントロールに設定する正しいデリゲートですか?
どんな助けでも大歓迎です。
要求されたコードを次に示します。
// AppDelegate.h
// SimpleApplication
#import <Cocoa/Cocoa.h>
#import "SimpleTest/SimpleTest.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextField *textField;
@end
// AppDelegate.m
// SimpleApplication
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Not much to do here for now.
}
// Breakpoint set in this function never fires.
- (void)controlTextDidChange:(NSNotification *)obj
{
NSMutableString* description= [[NSMutableString alloc] init];
id aDelegate= [_textField delegate];
Class delegateClass= [aDelegate class];
[description setString:[delegateClass description]];
[description release];
}
// To provide some information about the delegates.
- (IBAction)textChange:(id)sender
{
NSTextField* theTextField= (NSTextField*)sender;
NSMutableString* description= [[NSMutableString alloc] init];
id aDelegate= [theTextField delegate];
Class delegateClass= [aDelegate class];
[description setString:[delegateClass description]];
[description release];
}
@end
これは、メイン ウィンドウの NSTextField の右クリック情報のショットです。
ID インスペクターはFile's OwnerをNSApplicationとして表示します。これは、textChange にブレークポイントを設定し、テキスト フィールドで return を押したときにデバッガーに表示されるものです。ただし、 controlTextDidChange の実装者であるselfはAppDelegateです。対照的に、モーダル ダイアログでは、selfとFile's OwnerはNSWindowControllerから派生した同じオブジェクトです。
つまり、メイン ウィンドウのコントロールに適切なデリゲートが割り当てられていないということです。どうすればよいでしょうか。