Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

staziz

macrumors newbie
Original poster
Jul 11, 2013
2
0
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.
Code:
// 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.
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,418
A sea of green
The code can't work as posted, in debug or any other mode.

Your timer action selector is set with this code:
Code:
* * * * myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
* * * * * * * * * * * * * * * * * * * * * * * * target:self selector:@selector([COLOR="Red"]statusTimerProc[/COLOR])
* * * * * * * * * * * * * * * * * * * * * * * userInfo:nil repeats:YES];
Note the spelling of the selector name.

The method is this code:
Code:
- (void)[COLOR="red"]lcStatusTimerProc[/COLOR]
{*
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.
 

staziz

macrumors newbie
Original poster
Jul 11, 2013
2
0
Sorry for the typo error with my code I have corrected it and it is running..

Code:
-(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]];
    }
    
}
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.