Cocoa:NSApp beginSheet设置应用程序委托? [英] Cocoa: NSApp beginSheet sets the application delegate?
问题描述
我想在我的应用程序中显示自定义工作表,但我认为我做错了。虽然一切似乎工作很好,我有一个奇怪的副作用。 (需要几个小时才弄清楚)。事实证明,每次我在我的应用程序中显示一个工作表,应用程序委托被设置为工作表的实例,因此我的控制器会被取消设置作为委托,导致各种问题。
I am trying to display a custom sheet in my application, but I think I am doing something wrong. While everything seems to be working just fine, I have a rather odd side-effect. (which took hours to figure out). It turns out that everytime I display a sheet in my application, the Application delegate gets set to the instance of the sheet, thus my Controller gets unset as the delegate causing all sorts of problems.
我创建了一个NIB文件,我称之为FailureSheet.xib。我在IB中布局了我的接口,然后创建了一个'NSWindowController'的子类,名为'FailureSheet.m',我设置为文件的所有者。这是我的FailureSheet类:
I've created a NIB file which I called FailureSheet.xib. I laid out my interface in IB, and then created a subclass of 'NSWindowController' called 'FailureSheet.m' which I set to the File's Owner. Here is my FailureSheet class:
#import "FailureSheet.h"
@implementation FailureSheet // extends NSWindowController
- (id)init
{
if (self = [super initWithWindowNibName:@"FailureSheet" owner:self])
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (IBAction)closeSheetTryAgain:(id)sender
{
[NSApp endSheet:[self window] returnCode:1];
[[self window] orderOut:nil];
}
- (IBAction)closeSheetCancel:(id)sender
{
[NSApp endSheet:[self window] returnCode:0];
[[self window] orderOut:nil];
}
- (IBAction)closeSheetCancelAll:(id)sender
{
[NSApp endSheet:[self window] returnCode:-1];
[[self window] orderOut:nil];
}
@end
现在这是我在我的Controller类中显示FailureSheet的方法:
Nothing complex going on here. Now this is how I display the FailureSheet in my 'Controller' class:
sheet = [[FailureSheet alloc] init];
[NSApp beginSheet:[sheet window]
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(failureSheetDidEnd:etc:etc:)
contextInfo:nil];
现在,如果我在显示我的表单之前记录[NSApp委托]是什么, -0x012345>哪个是对的。然后,运行此代码和我的工作表后,如果我再次记录它是< FailureSheet-0xABCDEF>。
Now if I log what the [NSApp delegate] is before displaying my sheet, it is <Controller-0x012345> which is correct. Then, after running this code and my sheet is up, if I log it again it is <FailureSheet-0xABCDEF>.
不知道我在做什么错误- 有任何想法吗?
Not sure what I'm doing wrong here - Any ideas?
推荐答案
这是其中一个I'm-an-idiot的答案。
This is one of those "I'm-an-idiot" answers.
结果我在某个时候,我不小心在应用程序和文件的所有者(FailureSheet)之间的工作表的NIB文件中设置它作为委托的连接。所以,每次它加载它覆盖了现有的委托连接我在MainMenu NIB文件。
Turns out I at some point I accidentally made a connection in my sheet's NIB file between the Application and the File's Owner (FailureSheet) setting it as the delegate. So, everytime it got loaded it overwrote the existing delegate connection I had in my MainMenu NIB file.
这篇关于Cocoa:NSApp beginSheet设置应用程序委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!