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

标签 objective-c macos cocoa nspanel

我创建了自定义 NSPanel 并用 Sheet 开始展示它。它上面没有任何关闭按钮,我想在 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
}

最佳答案

试试这个:

[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];
}

关于objective-c - 在 Cocoa 中使用 NSTimer 关闭自定义 NSPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18502229/

相关文章:

iphone - 在 iphone 应用程序中安装的设置 View

iOS : containsObject Check for String

objective-c - NSSearchField:如何清除选择(设置选定范围)

cocoa - libusb 是 Mac OS X 上访问 USB 设备的首选方法吗?

objective-c - 任何一对多对多谓词

objective-c - 在不应该发生的时候双重释放

iOS:一次设置多个按钮的透明度

macos - Cocoa CALayer 不显示 NSImage

objective-c - std::pair 类似于 cocoa 或基础工具包?

cocoa - 复杂iPad应用程序中的MVC设计模式: is one fat controller acceptable?