web-dev-qa-db-ja.com

ARCを使用したXcode4(OSX 10.7.2)でのCocoaウィンドウのシートとしての表示

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

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

Appleドキュメントを含め、ほとんどのガイドはかなり時代遅れです。また、自動参照カウントやXcode4インターフェイスと互換性のあるものはないようです。

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

必要に応じて、お気軽に詳細をお尋ねください。

35
James Bellamy

Xcode 4のチュートリアル

新しいプロジェクトを作成し、以下を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

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

Xcode

CloseApp Delegateに接続します。

Xcode

そして、endTheSheetを選択します。

Xcode

OpenApp Delegateに接続します。

Xcode

そして、showTheSheetを選択します。

Xcode

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

Xcode

そして、theSheetを選択します。

Xcode

NSPanelを選択し、Visible At Launchを無効にします。 (必須のステップ!)

Xcode

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

Xcode

94
Anne

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

これにより、上記の呼び出しからのcompletionHandlerが起動します。ここで、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
6
james_alvarez