2012-10-10 22 views
1

我对OSX开发非常陌生。考虑这是我的第一个应用程序。我想在主窗口上单击按钮时显示工作表。我使用笔尖工作表在OSX上无法正常显示

以下是我的.h文件中

#import <Foundation/Foundation.h> 
#import "WebKit/Webkit.h" 

@interface MainViewObject : NSObject 
- (IBAction)accountButtonPressed:(id)sender; 
- (IBAction)cancelSheetButtonPressed:(id)sender; 

.m文件代码如下

#import "MainViewObject.h" 
@implementation MainViewObject 

- (IBAction)accountButtonPressed:(id)sender { 
    [NSApp beginSheet:self.accountSheet 
    modalForWindow:[mainWindowView window] 
    modalDelegate:nil 
    didEndSelector:nil 
     contextInfo:nil]; 

[NSApp runModalForWindow:self.accountSheet]; 

[NSApp endSheet:self.accountSheet]; 
[self.accountSheet orderOut:self]; 
} 

- (IBAction)cancelSheetButtonPressed:(id)sender { 
// Return to normal event handling 
[NSApp endSheet:self.accountSheet]; 
// Hide the sheet 
[self.accountSheet orderOut:sender]; 
} 

当我运行应用程序,我得到这样的:

http://i.imgur.com/DzJJ6.png

我被卡住了,我不知道这个错误。我无法获取表格,甚至无法关闭应用程序。我在网上提到了一些例子。

回答

1
- (IBAction)accountButtonPressed:(id)sender { 
    [NSApp beginSheet:self.accountSheet 
    modalForWindow:[mainWindowView window] 
    modalDelegate:nil 
    didEndSelector:nil 
     contextInfo:nil]; 

    [NSApp runModalForWindow:self.accountSheet]; 

    [NSApp endSheet:self.accountSheet]; 
    [self.accountSheet orderOut:self]; 

} 

哇,看看这个,截图看起来并不奇怪。

让我们一次一行地浏览一行。当您单击帐户按钮,你连续做马上4两件事:

  1. 你告诉应用程序开始显示连接到你的主窗口的工作表。这是可以的,并且实际上是您想要的唯一代码,即accountButtonPressed:方法。

  2. 紧接着开始该工作表之后,您会告诉应用程序您还希望以自己的方式显示该工作表(不附加到任何窗口,但在屏幕中间右侧),以应用程序模式方式显示该工作表在应用程序中处理所有其他事件。换句话说,这条线并不合理。您可以以“文档模式”方式(仅与工作表连接的窗口关联)或“应用程序模式”方式显示窗口,但不能同时显示。 ;-)

  3. 刚刚显示工作表后,您立即告知NSApp停止显示工作表。现在,你最终想做到这一点,但是在刚刚显示它之后0.0005秒就会关闭表单,这可能会让用户感到有点沮丧。

  4. 您现在告诉表单以隐藏自身。这需要从您的didEndSelector:方法完成,这会将我们带入第一种方法中的问题。

-

[NSApp beginSheet:self.accountSheet 
    modalForWindow:[mainWindowView window] 
    modalDelegate:nil 
    didEndSelector:nil 
     contextInfo:nil]; 

这是好的,但阅读文档beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:,也是Sheet Programming Topics: Using Custom Sheets。 (Class Reference页面顶部的Companion指南链接对于学习如何在现实世界中使用API​​特别有帮助,当我学习时它们非常有帮助)。

modalDelegate:指定nil意味着您没有任何东西等待通知有关何时显示表单停止显示的情况(当您拨打[NSApp endSheet:sheet]时会发生这种情况)。您还没有指定在工作表结束时要调用的@selector。选择器就像一个函数,又叫做“方法”。

您的代码应该是这个样子:

@implementation MDAppDelegate 

- (IBAction)showSheet:(id)sender { 
    [NSApp beginSheet:self.sheet 
     modalForWindow:self.window 
     modalDelegate:self 
     didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) 
      contextInfo:NULL]; 
} 

- (IBAction)cancel:(id)sender { 
    [NSApp endSheet:self.sheet]; 
} 

- (IBAction)ok:(id)sender { 
    [NSApp endSheet:self.sheet]; 
} 

- (void)sheetDidEnd:(NSWindow *)sheet 
      returnCode:(NSInteger)returnCode 
       contextInfo:(void *)contextInfo { 
    [sheet orderOut:nil]; 
} 
@end 

在本例中,单击显示表按钮,并且纸张开始被显示连接到主窗口。在工作表中,有一个“取消”和一个“确定”按钮,它们都会调用它们各自的方法。在这些方法中,您可以拨打[NSApp endSheet:self.sheet]。这告诉NSApp它应该在指定的对象上调用sheetDidEnd:returnCode:contextInfo:方法作为模式委托。然后在sheetDidEnd:returnCode:contextInfo:中告诉表单以隐藏自己。

编辑:

每个NSWindow具有可在Interface Builder中设置一个“可见在启动”标志。如果设置了此标志,则在加载nib文件时窗口将可见。如果未设置,窗口将隐藏,直到以编程方式显示它。就在笔尖文件编辑的标志像图所示:

enter image description here

+0

我用你最后给出的代码。当我运行应用程序时,我发现在点击按钮获取表单之前,两个窗口都显示出来。这里是同一个http://i.imgur.com/XWNSk.png – Nik

+0

关于如何最初隐藏工作表窗口的任何想法的快照,因为我发现如果关闭默认打开的工作表(如以前的注释链接所示)并按下它的按钮工作正常 – Nik

+0

@Raj:更新的答案 – NSGod