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

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 12, 2013, 02:31 AM   #1
staziz
macrumors newbie
 
Join Date: Jul 2013
Problem with Model window and NSTimer.

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 maflynn; Jul 12, 2013 at 01:21 PM.
staziz is offline   0 Reply With Quote
Old Jul 12, 2013, 12:32 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
use code tags around your code so it'll format properly.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Jul 12, 2013, 01:30 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
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(statusTimerProc)
* * * * * * * * * * * * * * * * * * * * * * * userInfo:nil repeats:YES];
Note the spelling of the selector name.

The method is this code:
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.
chown33 is offline   0 Reply With Quote
Old Jul 15, 2013, 02:13 AM   #4
staziz
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
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 stridemat; Jul 15, 2013 at 03:24 PM. Reason: Added code tags
staziz is offline   0 Reply With Quote

Reply
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
Some Apple Retail Stores Offering 30-Day Window to Exchange iPads for New Model MacRumors MacRumors.com News Discussion 268 Sep 16, 2014 02:59 AM
NSTimer Alternatives adimanic iPhone/iPad Programming 1 Jan 4, 2014 01:04 AM
NStimer in Background erdinc27 iPhone/iPad Programming 6 May 11, 2013 08:21 AM
NSTimer not firing eddjc Mac Programming 6 Jul 28, 2012 04:30 PM
Circular release problem with NSTimer... ArtOfWarfare iPhone/iPad Programming 3 Jun 11, 2012 08:04 PM

Forum Jump

All times are GMT -5. The time now is 10:23 PM.

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

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