2013-08-29 24 views
0

我創建了自定義NSPanel,並使用Sheet顯示它的bengin。在它上面沒有任何按鈕可以關閉,我想在10秒後用NSTimer關閉這個面板。我怎樣才能做到這一點?使用NSTimer在可可中關閉自定義NSPanel

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

回答

1

T ry this:

[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]; 
} 
0
[NSApp endSheet:sheet]; 
[NSApp orderOut:nil]; 
+0

對不起,我不知道你的代碼。它用於添加NStimer還是? –

0

試試這個: - (空)closePanel:(的NSTimer *)theTimer {

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

}

相關問題