35

ログインウィンドウをメインウィンドウからシートとして表示しようとしていますが、AppKitメソッドを実装しようとすると、さまざまな区別できない理由で常にエラーが表示されます。

そこにあるオンラインガイドはどれも機能していません。コード/適応クラスを自分のプロジェクトに適用すると、機能しません。

Appleのドキュメントを含め、ほとんどのガイドはかなり古くなっています。そして、それらのどれも自動参照カウントと互換性がないようです。またはXcode4インターフェース。

メインウィンドウでボタンを押した後にシートを表示する最も簡単な方法について、誰かが私のために完全なガイドを詳しく説明できるでしょうか。

必要に応じて、詳細についてお気軽にお問い合わせください。

4

2 に答える 2

94

Xcode4のチュートリアル

新しいプロジェクトを作成し、以下をとに追加しAppDelegate.hますAppDelegate.m

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {

    IBOutlet NSPanel *theSheet;
}

@property (assign) IBOutlet NSWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (IBAction) showTheSheet:(id)sender {

    [NSApp beginSheet:theSheet
       modalForWindow:(NSWindow *)_window
        modalDelegate:self
       didEndSelector:nil
          contextInfo:nil];

}

-(IBAction)endTheSheet:(id)sender {

    [NSApp endSheet:theSheet];
    [theSheet orderOut:sender];

}

@end

を開きますMainMenu.xib
既存のを使用しNSWindowます。
次のボタンを使用して表示します。

Xcode

新しいを1つ作成しますNSPanel
適切なを追加しますNSButtons

Xcode

に接続CloseしますApp Delegate

Xcode

そして、を選択しますendTheSheet

Xcode

に接続OpenしますApp Delegate

Xcode

そして、を選択しますshowTheSheet

Xcode

App Delegateを新しいに接続しますNSPanel

Xcode

そして、を選択しますtheSheet

Xcode

を選択しNSPanelて無効にしVisible At Launchます。 (基本的なステップです!)

Xcode

今すぐ実行を押して、結果を楽しんでください:

Xcode

于 2011-11-09T01:02:30.343 に答える
6

SDK 10.10で状況が変更されました。呼び出しは、理解しやすいと思います。親ウィンドウは、子NSWindowをシートとして起動する役割を果たします。次に、この子NSWindowをNSAppに渡して、モーダルで実行します。次に、逆の操作を行ってアンラップします。

表示シート

呼び出す代わりにシートを表示するには:

[NSApp beginSheet:theSheet
   modalForWindow:(NSWindow *)_window
    modalDelegate:self
   didEndSelector:nil
      contextInfo:nil];

ここで、親ウィンドウを呼び出します。

(void)beginSheet:(NSWindow *)sheetWindow
 completionHandler:(void (^)(NSModalResponse returnCode))handler

そして、モーダルループのようにシートを実行するには、次のコマンドでNSAppを呼び出す必要もあります。

- (NSInteger)runModalForWindow:(NSWindow *)aWindow

クロージングシート

シートを閉じるには、親ウィンドウを呼び出します。

- (void)endSheet:(NSWindow *)sheetWindow

これにより、上記の呼び出しからのcompleteHandlerが起動します。ここで、次のコマンドを使用してNSAppを呼び出すことにより、モーダルウィンドウの実行を停止する呼び出しを行うことができます。

- (void)stopModalWithCode:(NSInteger)returnCode

完全な例

@implementation AppDelegate

@synthesize window = _window;

- (IBAction) showTheSheet:(id)sender {

    [_window beginSheet: theSheet
         completionHandler:^(NSModalResponse returnCode) {
             [NSApp stopModalWithCode: returnCode];
         }];

    [NSApp runModalForWindow: theSheet];

}

-(IBAction)endTheSheet:(id)sender {
    [_window endSheet: theSheet];
}

@end
于 2015-04-28T18:27:21.787 に答える