2015-01-08 43 views
0

我的主窗口上有一個按鈕,打開另一個窗口來收集用戶輸入。如果用戶完成輸入,窗口應該關閉。如果用戶點擊紅色按鈕,我需要顯示警報並讓窗口關閉。無論哪種情況,我都需要能夠重新打開輸入窗口。重新打開一個窗口並確定它是如何關閉的

我可以第一次打開窗口,但不能以編程方式關閉它,也不能重新打開它。我已閱讀所有可找到的帖子,但沒有找到可以應用於我的案例的帖子。我認爲我的問題是一個出口,並委託。

該窗口的代表是Calibrate。

Calibrate.h

#import <Cocoa/Cocoa.h> 
#import <Foundation/Foundation.h> 
. 
. 
@interface Calibrate : NSWindowController 
. 
. 
@end 

Calibrate.m

#import "Calibrate.h" 
Calibrate *calibrate; 

- (IBAction)showCalibratePanel:(id)sender 
{ 
    [calibrate showWindow:self]; 
} 

- (void)handleMaxAngleChange:(NSNotification *)notification 
{ 
    ///// last step in info gathering 
    NSString *s = [[notification userInfo]objectForKey:@"myMaxAngleKey"]; 
    gotResponse = NO; 
    [calibrate close]; //////////// does not close. 
} 


#import "Calibrate.h" 
@interface Calibrate() 
@end 

@implementation Calibrate 
. 
. 
- (id)init 
{ 
    self = [super initWithWindowNibName:@"Calibrate"]; 
    return self; 
} 

- (void)windowDidLoad 
{ 
    [super windowDidLoad]; 
} 

-(BOOL)windowShouldClose:(NSNotification *)note 
{ //// fires when red button clicked but alert not shown. 
    NSAlert *alert = [[NSAlert alloc] init]; 
    [alert setMessageText:@"Incomplete calibration may cause ERC to not work correctly."]; 
    [alert beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow 
             modalDelegate:nil 
             didEndSelector:nil  
             contextInfo:nil]; 

    return YES; 
} 
+0

我認爲你應該看看在一張紙而不是一個新窗口中呈現這種事情.. – Jef

+0

@Jef - 我以前沒有使用過紙張。有什麼優勢? – Mike

+0

工作表只是一個連接到特定窗口的對話框,確保用戶永遠不會丟失對話框屬於哪個窗口。將對話框連接到相關窗口的功能使用戶能夠充分利用OS X窗口分層模型,並鼓勵無模式;用戶可以在工作表打開時處理其他文檔或其他應用程序。 (來自蘋果文檔https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Sheets/Concepts/AboutSheets.html#//apple_ref/doc/uid/20001043-BABFIBIA) – Jef

回答

0

使用

[calibre.window close]; 

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/#//apple_ref/occ/instm/NSWindow/close

要顯示窗口使用:

[calibrate showWindow:self]; 

和你一樣。

+0

對不起,但沒有奏效。 OrderOut也沒有。 – Mike

+0

我做了Xcode項目供你試用。告訴我,如果smthg。是錯的。 http://tagtaxa.com//download/window.zip –

+0

你的例子工作完美。我將你的代碼添加到我的程序中並取得了一些進展。紅色按鈕關閉校準窗口並顯示警報。我仍然無法關閉ORSSerialPortController(您的MainWindowController)的Calibrate窗口,也無法重新打開它。在我的ORSSerialPortController @interface ORSSerialPortDemoController:NSObject的是從yours.I不同不能夠實現WindowDidLoad。 – Mike

相關問題