PDA

View Full Version : Problem with Model window and NSTimer.




staziz
Jul 12, 2013, 01:31 AM
I am having a problem with Model window and NSTimer.

I am creating a window and running it as ModalForWindow. I am also creating NSTimer for some periodic purpose. When I am finished I stop the model in the NSTimer handler. However the control doesn't return the position where I have started the modalWindow. It only returns if I hover the mouse around the main application window or click somewhere.
When I try to debug it works fine.
Here is a sample code: In this code the NSLog (*NSLog(@"after [NSApp runModalForWindow:dialogWindow]");) is not hit unless I click somewhere in the application.

// Here I start the ModalWindow
-(void) loadDialog
{
dialogWindow = [self window];
[[self window] setDelegate:self];* **

*[NSApp runModalForWindow:dialogWindow];
*NSLog(@"after [NSApp runModalForWindow:dialogWindow]");
** *
* [dialogWindow close];
* NSLog(@"after [dialogWindow close];");
}

// Here I create NSTimer and attach to currentRunLoop

- (void)windowDidBecomeMain:(NSNotification *)notification {* **
* * if (myTimer == nil) {
* * * * myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
* * * * * * * * * * * * * * * * * * * * * * * * target:self selector:@selector(statusTimerProc)
* * * * * * * * * * * * * * * * * * * * * * * userInfo:nil repeats:YES];
* * * * [[NSRunLoop currentRunLoop] addTimer:myTimer
* * * * * * * * * * * * * * * * * forMode:NSModalPanelRunLoopMode];
* * }
}
// NSTimer Handler where I stop Modal after some intervals

- (void)lcStatusTimerProc
{*
* * counter++;
* * if(counter == 20) {
* * * * [myTimer invalidate];
* * * * myTimer = nil;
** * * *
* * * * [NSApp stopModal];
* * }
* * else {
* * * * [label setStringValue:[NSString stringWithFormat:@"%u", counter]];
* * }* **
}

Any suggestions whether I am doing something wrong.



ArtOfWarfare
Jul 12, 2013, 11:32 AM
use code tags around your code so it'll format properly.

chown33
Jul 12, 2013, 12:30 PM
The code can't work as posted, in debug or any other mode.

Your timer action selector is set with this code:
* * * * myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
* * * * * * * * * * * * * * * * * * * * * * * * target:self selector:@selector(statusTimerProc)
* * * * * * * * * * * * * * * * * * * * * * * userInfo:nil repeats:YES];

Note the spelling of the selector name.

The method is this code:
- (void)lcStatusTimerProc
{*

Note the spelling of the name.

First, there's a prefixed lc, so the timer action selector won't match for that reason. Second, the S is capitalized. Objective-C (like C) is case-sensitive. The names must be exactly the same in both places, letter-for-letter, and case-for-case.

So either this code doesn't work as claimed, or you've made changes to it when posting it.
Always post code exactly as it exists in your file.
And always use CODE tags (http://forums.macrumors.com/showthread.php?t=747660).

staziz
Jul 15, 2013, 01:13 AM
-(void) loadDialog
{
dialogWindow = [self window];
[[self window] setDelegate:self];

[NSApp runModalForWindow:dialogWindow];
NSLog (*NSLog(@"after [NSApp runModalForWindow:dialogWindow]")

[dialogWindow close];
NSLog(@"loadDialog : after [dialogWindow close];");
}


- (void)windowDidBecomeMain:(NSNotification *)notification {
#define kTimerPeriod (1.0 / 4.0)

if (myTimer == nil) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:kTimerPeriod
target:self selector:@selector(statusTimerProc)
userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:myTimer
forMode:NSModalPanelRunLoopMode];
}
}

- (void)statusTimerProc
{

counter++;

if(counter == 20) {
[myTimer invalidate];
myTimer = nil;

[NSApp stopModal];
}
else {
[label setStringValue:[NSString stringWithFormat:@"%u", counter]];
}

}