Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Jun 15, 2009, 04:20 AM   #1
macrumors newbie
Join Date: Feb 2009
Close NSAlert after some time using Timer

I want to close a alert Window (NSAlert) after specific period of time if user
does not click on any of the buttons.
Is there a way to set up a NSTimer to close the Alert Panel after 10 secs.

I am doing following thing and its working. But I know this wrong way to do this.
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle: @"OK"];
[alert setMessageText: @"Attention!!! This a critical Alert."];
[alert setAlertStyle: NSInformationalAlertStyle];

NSTimer *myTimer = [NSTimer timerWithTimeInterval: 5
selector: @selector(killWindow

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

int choice = 0;
choice = [alert runModal];
if(choice != 0)
[myTimer invalidate];
-(void) killWindowNSTimer *) theTimer
[[alert window] close];

Can anyone please suggest the best way to do this.

Tejashree is offline   0 Reply With Quote
Old Jun 15, 2009, 11:22 AM   #2
macrumors 601
HiRez's Avatar
Join Date: Jan 2004
Location: Western US
Why do say you know it's wrong? You said it works, right? Only thing I can see is you set of for 5 seconds when you said you wanted 10.
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jun 16, 2009, 01:19 AM   #3
macrumors 6502a
mathcolo's Avatar
Join Date: Sep 2008
Location: Massachusetts
Yeah, if it's working for you then I wouldn't bother changing it. Despite that, that's how I'd do it; an NSTimer controlling NSAlert.

Sometimes you have to just let code be code
- 13" rMBP - 2.6 GHz i5 - 512GB SSD - 8GB RAM
- Google Nexus 5
mathcolo is offline   0 Reply With Quote
Old Mar 22, 2013, 12:27 PM   #4
macrumors newbie
Join Date: Jan 2013
I used this same code, but when I try to close with a timer, the rest of my app freezes. Also, if I try to do NSApp.stopModal to stop running Modal, the app crashes. Any ideas on the correct way to do this?
ruchi830 is offline   0 Reply With Quote
Old Mar 22, 2013, 02:23 PM   #5
macrumors G5
gnasher729's Avatar
Join Date: Nov 2005
I'd try abortModal instead of stopModal, that's what the documentation says. And your memory management seems dubious. [autorelease] should release the alert before the next round of the run loop, so by the time the timer fires, the alert should be gone. What does the static analyzer think of your code?
gnasher729 is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Reverting to 10.8 from mavericks via time machine- first timer questions jkaz OS X Mavericks (10.9) 5 Jan 13, 2014 12:02 AM
Shake:Time - gesture based iOS timer / flaaat andrewPaw iPhone and iPod touch Apps 1 Jun 25, 2013 02:04 PM
Is there a way to close LOTS of windows at the same time? stanw Mac OS X 10.7 Lion 1 May 22, 2013 10:15 PM
It's got to be close to time to switch if you need power lloyd709 Mac Pro 13 Dec 14, 2012 05:50 PM
Issue with NSAlert text arunsm OS X 0 Aug 16, 2012 04:54 AM

Forum Jump

All times are GMT -5. The time now is 07:26 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC