「アクティブなサブビュー」を追跡するNSplitViewを使用してココアアプリを作成しようとしています(ターミナルアプリが編集中のペインを認識する方法など)。
ユーザーが最後にクリックした PaneView に基づいて「currentPaneContainerViewController」を追跡する SessionWindowController があります。
いくつかのクラスとファイル:
SessionWindowController.h/.m
PaneContainerViewController.h/.m
PaneView.h/.m
PaneContainerView.xib
PaneContainerView.xib には、そのファイル所有者として PaneContainerViewController があります。
私の現在の実装は次のとおりです。
NSView から PaneContainerViewController にアクセスするには、ファイル所有者を参照する IBOutlets を使用し、SessionWindowController にアクセスするには、作成したデリゲート メソッドに準拠するオブジェクトの IBOutlet も維持します (つまり、オブジェクトはたまたま SessionWindowController になります)。
#import <Cocoa/Cocoa.h>
#import "PaneViewDelegate.h"
@class PaneContainerViewController;
@class SessionWindowController;
@interface PaneView : NSView
{
//outlet connection to own controller
IBOutlet PaneContainerViewController *myPaneContainerViewController;
//we'll use delegation to get access to the SessionWindowController
IBOutlet id<PaneViewDelegate> sessionDelegate;
}
@implementation PaneView
-(void)mouseUp:(NSEvent *)event
{
if (sessionDelegate && [sessionDelegate respondsToSelector:@selector(setCurrentPaneContainerViewController:)]) {
[sessionDelegate setCurrentPaneContainerViewController:myPaneContainerViewController];
}
}
以下は sessionDelegate が属するクラスで、PaneViewDelegate プロトコルに準拠しています。
@interface SessionWindowController : NSWindowController <PaneViewDelegate>
{
PaneContainerViewController *currentPaneContainerController;
}
- (void)setCurrentPaneContainerViewController:(PaneContainerViewController*)controller;
問題は、IBOutlet を使用して SessionWindowController オブジェクトにアクセスすることです。Interface Builder で、sessionDelegate アウトレットを何に接続して、SessionWindowController インスタンスにアクセスできるようにしますか? さらに、コントローラーを NSEvent の代わりにデリゲートに渡しても問題ありませんか?
私は Cocoa を初めて使用するので、より良いデザイン パターンがあれば教えてください。これは、かなり一般的な機能の定型文のように思えます。