2015-01-08 45 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

相关问题