Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 12, 2013, 01: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 12:21 PM.
staziz is offline   0 Reply With Quote
Old Jul 12, 2013, 11:32 AM   #2
ArtOfWarfare
macrumors 601
 
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.
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   0 Reply With Quote
Old Jul 12, 2013, 12: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, 01: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 02: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:27 PM.

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

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