我的主窗口上有一個按鈕,打開另一個窗口來收集用戶輸入。如果用戶完成輸入,窗口應該關閉。如果用戶點擊紅色按鈕,我需要顯示警報並讓窗口關閉。無論哪種情況,我都需要能夠重新打開輸入窗口。重新打開一個窗口並確定它是如何關閉的
我可以第一次打開窗口,但不能以編程方式關閉它,也不能重新打開它。我已閱讀所有可找到的帖子,但沒有找到可以應用於我的案例的帖子。我認爲我的問題是一個出口,並委託。
該窗口的代表是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;
}
我認爲你應該看看在一張紙而不是一個新窗口中呈現這種事情.. – Jef
@Jef - 我以前沒有使用過紙張。有什麼優勢? – Mike
工作表只是一個連接到特定窗口的對話框,確保用戶永遠不會丟失對話框屬於哪個窗口。將對話框連接到相關窗口的功能使用戶能夠充分利用OS X窗口分層模型,並鼓勵無模式;用戶可以在工作表打開時處理其他文檔或其他應用程序。 (來自蘋果文檔https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Sheets/Concepts/AboutSheets.html#//apple_ref/doc/uid/20001043-BABFIBIA) – Jef