Cocoa:NSApp beginSheet 设置应用程序委托?
我正在尝试在我的应用程序中显示自定义工作表,但我认为我做错了什么。虽然一切似乎都工作得很好,但我有一个相当奇怪的副作用。 (花了几个小时才弄清楚)。事实证明,每次我在应用程序中显示工作表时,应用程序委托都会设置为该工作表的实例,因此我的控制器作为委托被取消设置,从而导致各种问题。
我创建了一个 NIB 文件,将其命名为 FailureSheet.xib。我在 IB 中布置了界面,然后创建了一个名为“FailureSheet.m”的“NSWindowController”子类,并将其设置为文件的所有者。这是我的 FailureSheet 类:
#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 的方式:
sheet = [[FailureSheet alloc] init];
[NSApp beginSheet:[sheet window]
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(failureSheetDidEnd:etc:etc:)
contextInfo:nil];
现在,如果我在显示我的工作表之前记录 [NSApp 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.
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
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];
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是“我是个白痴”的答案之一。
事实证明,我在某个时候不小心在工作表的 NIB 文件中在应用程序和文件所有者 (FailureSheet) 之间建立了连接,并将其设置为委托。因此,每次加载它时,它都会覆盖 MainMenu NIB 文件中现有的委托连接。
This is one of those "I'm-an-idiot" answers.
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.