我想出了如何做到这一点。希望可以发帖。。
主窗口.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;
主窗口.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 中新增的这种方法,我不需要特殊的工作表控制器,并且控制保持在本地引用。