Close custom NSPanel with NSTimer in Cocoa

869 views Asked by At

I created custom NSPanel and I show it bengin with Sheet. On it do not have any button to close, I want to close this panel with NSTimer after 10s. How can I do that?

[[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 () function :

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

There are 3 answers

0
Hussain Shabbir On BEST ANSWER

Try 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
Dhanunjay Kumar On

Try This : -(void) closePanel: (NSTimer *) theTimer {

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

}

0
atomkirk On
 [NSApp endSheet:sheet];
 [NSApp orderOut:nil];