麻烦使用自定义模态表与Cocoa

时间:2010-11-01 18:22:15

标签: objective-c macos cocoa cocoa-sheet

我的目标:当应用程序通过冗长的循环时,显示带有确定NSProgressIndicator的自定义工作表。我希望工作表是应用程序模式,而不是文档模式。用户无法关闭模态表。他们必须等待应用程序完成循环处理。

问题:我无法将自定义工作表附加到窗口。它显示为缺少窗口标题栏的单独窗口(如表单所示)。此外,当循环结束时,纸张不会释放(不会关闭)。

我有两个单独的nib文件用于工作表和主应用程序窗口,还有两个控制器类用于每个窗口。

以下是相关信息: 自定义工作表的控制器实现:

@implementation ProgressSheetController //subclass of NSPanel

-(void)showProgressSheet:(NSWindow *)window
{
    //progressPanel is an IBOutlet to the NSPanel
    if(!progressPanel)
        [NSBundle loadNibNamed:@"ProgressPanel" owner:self];

    [NSApp beginSheet: progressPanel
       modalForWindow: window
        modalDelegate: nil
       didEndSelector: nil
          contextInfo: nil];

    //modalSession is an instance variable
    modalSession = [NSApp beginModalSessionForWindow:progressPanel];

    [NSApp runModalSession:modalSession];
}

-(void)removeProgressSheet
{
    [NSApp endModalSession:modalSession];
    [NSApp endSheet:progressPanel];
    [progressPanel orderOut:nil];
}

//some other methods 
@end

主应用程序窗口的实现。 testFiles方法是一个连接到按钮的IBAction。

@implementation MainWindowViewController //subclass of NSObject

-(IBAction)testFiles:(id)sender;
{
    //filesToTest is a mutable array instance variable
    int count = [filesToTest count];

    float progressIncrement = 100.0 / count;

    ProgressSheetController *modalProgressSheet = [[ProgressSheetController alloc] init];
    [modalProgressSheet showProgressSheet:[NSApp mainWindow]];

    int i,
    for(i=0; i<count; i++)
    {
        //do some stuff with the object at index i in the filesToTest array

        //this method I didn't show in ProgressSheetController.m but I think it's self explanatory
        [modalProgressSheet incrementProgressBarBy:progressIncrement];
    }
    //tear down the custom progress sheet
    [modalProgressSheet removeProgressSheet];
    [modalProgressSheet release];
}
@end

一想法:我是否正确地进行了分类?我应该使用NSWindowController吗?提前感谢您的帮助!

2 个答案:

答案 0 :(得分:15)

发现this gem正在做一些谷歌搜索。 Interface Builder中我的NSPanel的行为设置为“在启动时可见”,这是使用IB时新窗口的默认值。发生的事情是,一旦装入笔尖,窗口就会在[NSApp beginSheet:...]之前显示出来。因此,取消选中“在启动时可见”选项可以解决我的问题,现在一张工作表就像我想要的那样连接到窗口。

答案 1 :(得分:1)

@implementation ProgressSheetController //subclass of NSPanel

-(void)showProgressSheet:(NSWindow *)window
{
    //progressPanel is an IBOutlet to the NSPanel
    if(!progressPanel)
        [NSBundle loadNibNamed:@"ProgressPanel" owner:self];

所以你的NSPanel拥有另一个NSPanel?如果第二个是进度面板,第一个显示的是什么?

  

我应该[继承] NSWindowController吗?

听起来像。

modalSession = [NSApp beginModalSessionForWindow:progressPanel];

为什么?

[modalProgressSheet showProgressSheet:[NSApp mainWindow]];

您确认有一个主窗口吗? mainWindow不会返回您最关心的窗口;它返回活动窗口(可能,但不一定也是键)。

如果mainWindow返回nil,则可能是您遇到问题的原因。

int i,
for(i=0; i<count; i++)
{
    //do some stuff with the object at index i in the filesToTest array

首先,int是错误的类型。您应该将NSUInteger用于NSArray索引。

其次,除非您需要其他内容的索引,否则应使用fast enumeration