如何在小牛队中打开NSSheet?

时间:2013-10-26 15:49:35

标签: cocoa nswindowcontroller osx-mavericks

在Mavericks中,打开和关闭NSSheets的方法已经改变。为了使其更加严格,Release Notes与当前文档(见下文)不匹配。

我正在尝试这样做:

MainSheetController(NSWindowController):

-(IBAction)callSheet:(id)sender {
    [sheetController openSheet];
}

SheetController(NSObject):

 (void)openSheet {  
    [[NSBundle mainBundle] loadNibNamed:sheetName owner:self topLevelObjects:nil];
    NSLog(@"1");
    [self.mainWindowController.window beginSheet:self.sheet completionHandler:nil];
    NSLog(@"2");    
}

我到2,没有错误或警告,但没有表..

当前文档:

#if NS_BLOCKS_AVAILABLE
- (void)beginSheet:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);
- (void)beginCriticalSheet:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);
#endif

3 个答案:

答案 0 :(得分:10)

- (IBAction)userButtonPressed:(id)sender {

    UserLoginWindowController * wc = [UserLoginWindowController new];
    // we keep a reference, so the WC doesn't deallocate
    self.modalWindowController = wc;

    [[self window] beginSheet:[wc window] completionHandler:^(NSModalResponse returnCode) {
        self.modalWindowController = nil;
    }];

}
UserLoginWindowController中的

- (IBAction)cancelButtonPressed:(id)sender {

    [[[self window] sheetParent] endSheet:[self window] returnCode:NSModalResponseCancel];

}

答案 1 :(得分:5)

- (IBAction)showSheet:(id)sender
{
    if (_windowController == nil)
    {
        _windowController = [MyWindowController new];
    }

    [[self window] beginSheet:[_windowController window] completionHandler:^(NSModalResponse returnCode)
    {
    }];
}

// And inside your MyWindowController class:

- (id)init
{
    self = [super initWithWindowNibName:@"MyWindowNibName"]; // TODO: Change the name of your NIB
    return self;
}

在你的nib文件中,确保窗口中的“Visible At Launch”标志未被取消。

答案 2 :(得分:1)

我想出了如何做到这一点。希望可以发布..

MainWindow.h

@interface MainWindowController : NSWindowController {
    NSString *sheetName;
    IBOutlet NSWindow *sheet;
    id result1;
    id result2;
    ...
    id resultn;
}

@property (strong) NSString *sheetName;
@property (strong, nonatomic) IBOutlet NSWindow *sheet;

-(IBAction)callSheet0:(id)sender;
-(IBAction)callSheet1:(id)sender;
-(IBAction)callSheetn:(id)sender;

- (void)openSheet;
- (IBAction)save:(id)sender;
- (IBAction)cancel:(id)sender;

MainWindow.m

- (void)windowDidLoad
{
    NSLog(@"%s", __FUNCTION__);
    [super windowDidLoad];
    sheetName = [[NSString alloc] init];
}


-(IBAction)callSheet0:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    sheetName = @"Sheet0";
    [self openSheet];
}
....

-(IBAction)callSheetn:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    sheetName = @"Sheetn";
    [self openSheet];


- (void)openSheet {
    NSLog(@"%s", __FUNCTION__);
    NSLog(@"sheetName: %@",sheetName );
    [[NSBundle mainBundle] loadNibNamed:sheetName owner:self topLevelObjects:nil];
    [self.window beginSheet:self.sheet completionHandler:nil];

}

- (void)save:(NSButton*)sender {

    switch ([sender tag]) {
        case 0:
            [self doSave1];
            result = @"1";
            break;

        case 1:
            [self doSave2];
            result = @"2";
            break;

        case n:
            [self doSaven];
            result = @"x";
            break;
    }
    [self endSheet:self.sheet returnCode:result];
}


- (IBAction)cancel:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    result = @"0";
    [self endSheet:self.sheet returnCode:result];
    // returnCode is optional
}

//endSheet:(NSWindow *)sheetWindow  {
- (void)endSheet:(NSWindow *)sheetWindow returnCode:returnCode {
    //NSLog(@"%s", __FUNCTION__);
    [sheetWindow orderOut:self];

}

- (void)save:(NSButton*)sender {

    switch ([sender tag]) {
        case 0:
            [self doSave1];
            result = @"1";
            break;

            case n:
            [self doSave3];
            result = @"3";
            break;
    }
    [self endSheet:self.sheet returnCode:result];
}

使用这种方法,10.9中的新方法,我不需要特殊的纸张控制器,控件仍然是本地引用。