在Cocoa中使用NSTimer关闭自定义NSPanel

时间:2013-08-29 04:22:24

标签: objective-c macos cocoa nspanel

我创建了自定义NSPanel,然后用Sheet显示它的bengin。在它没有任何关闭按钮,我想在10秒后用NSTimer关闭此面板。我怎么能这样做?

[[NSApplication sharedApplication] beginSheet: scanningPanel
                               modalForWindow: window
                                modalDelegate: self
                               didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
                                  contextInfo: nil];

[[NSApplication sharedApplication] runModalForWindow: scanningPanel];
NSTimer *myTimer = [NSTimer timerWithTimeInterval: 10.0
                                               target:self
                                             selector: @selector(closePanel:) userInfo:nil
                                              repeats:NO];

    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode];

closePanel()函数:

-(void) closePanel: (NSTimer *) theTimer
{
    NSLog(@"closePanel");
    [scanningPanel abortModal]; // seems it not work
}

3 个答案:

答案 0 :(得分:1)

 [NSApp endSheet:sheet];
 [NSApp orderOut:nil];

答案 1 :(得分:1)

试试这个:

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

NSTimer *tm=[NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(closePanel:)
                                           userInfo:nil
                                            repeats:NO];

- (void)closePanel:(NSTimer *)theTimer
{
    NSLog(@"closePanel");
    [NSApp endSheet:scanningPanel];
    [scanningPanel orderOut:self];
}

答案 2 :(得分:0)

试试这个: - (void)closePanel :( NSTimer *)theTimer {

 [scanningPanel orderOut:self];
 [NSApp stopModal];

}