Lefora Free Forum
login join
Loading
494 views

NSAlert with NSTimer

Page 1
1–2
rookie - member
2 posts


Hi,

 I am facing a problem to display NSAlertPanel dialog with NSTimer.

Actually, I need to display a alert panel which update the time. My problem is- it is not updating the time until I closed the panel. Here is the code I am using-

[self remainingTime: 30];

- (void) remainingTime: (int) timeOut

{

msg = [NSString stringWithFormat: @"%d seconds remaining", timeOut];


NSRunAlertPanel(@"temp",msg , @"Accept", nil, @"Reject");


NSTimer* myTimer = [NSTimer timerWithTimeInterval: 1

target: self selector: @selector(timeUpdater:)

userInfo : nil repeats: NO];


[myTimer fire];

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


}


- (void) timeUpdater: (NSTimer*) theTimer

{

timeOut--;

if(timeOut <= 0)

        [theTimer invalidate];


[self displaySeconds: timeOut];

}


any help will be highly appreciated

Thanks!

superstar - member
233 posts


I'm not sure I understand what this code is to achieve, so allow me to guess.  I think you're trying to display an Alert with "<n> seconds remaining" 

<n> counts down from 30 seconds until the user clicks, or zero is reached (it self dismisses when zero is reached).

However NSRunAlertPanel runs a modal dialog - so it will stay on the screen until the user clicks and then your code create a timer which fires into a vacuum.

So, I think you're going to have to invent your own alert box (in a nib) and use
(IBOutlet) NSTextField* msg ; 
and
- (IBAction) close : (id) sender 

in your AlertController object.  You can change the text displayed in the usual way with [msg setObjectValue:string]; when you are called by the timer. 

close() can do a [[self window]close] to dismiss your home-made alert dialog (and invalidate and release the timer).  Your timeout code can call close() if the user doesn't dismiss the alert.

I'm rather surprised by this.  It seems more complicated than I would have expected.  However there seems to be no obvious way to gain access to the NSTextView* of the strings in the NSRunAlertPanel window.  Of course, it may be possible to find the alert box (by enumerating all the windows of the current app), then enumerate the alert box controls - however I think it's easier to provide your own alert box.

__________________
Page 1
1–2

Locked Topic


You must be a member to post in this forum

Join Now!