Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Loop control via NSButton

Loop control via NSButton
Thread Tools
Junior Member
Join Date: Dec 1999
Location: Clovis' underwater chicken ranch
Status: Offline
Reply With Quote
Aug 13, 2003, 06:29 PM
 
I've been reading on eventLoops and still don't understand how I can start/stop an "infinite" loop via one (or two) NSButtons.

I have my action that a "Start" button is attached to, and in it I can change "Start" to "Stop". I could, at this time, start the loop, but I want to be able to click "Stop" to (naturally) stop the loop.

I could use NSTimer to run the loop (as opposed to using two threads) but how do I exit this Action when the "Stop" button is clicked?
The empty can rattles the loudest
     
Junior Member
Join Date: Dec 1999
Location: Clovis' underwater chicken ranch
Status: Offline
Reply With Quote
Aug 13, 2003, 07:10 PM
 
I just found this at
http://www.omnigroup.com/mailman/arc...er/004964.html


<quote>
- (void)startNSButton *)sender
{
if (!_timer) {
[sender setTitle:@"Stop"];
[sender setAction:@selector(stop];
_timer = [[NSTimer scheduledTimerWithTimerInterval:30 target:self
selector:@selector(timerFired) userInfo:nil repeats:YES] retain];
}
}

- (void)stopNSButton *)sender
{
if (_timer) {
[sender setTitle:@"Start"];
[sender setAction:@selector(start];
[_timer release];
_timer = nil;
}
}
</quote>

Is that what I'm looking for?
The empty can rattles the loudest
     
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Aug 13, 2003, 07:53 PM
 
I'm not exactly sure about your needs but below is some code that works for me in a perhaps closely related situation:

Code:
// //From the controlling class, you could implement the following two methods. // Before your UI is available to the user, this method is triggered automatically - (void) awakeFromNib { startStopFlag = YES; } // the is action is the associated with an NSButton - (IBAction)toggleOnOff: (id)sender { if(startStopFlag){ [viewOutlet start]; [OnOffButton setTitle:@"Stop"]; startStopFlag = NO; }else{ [viewOutlet stop]; [OnOffButton setTitle:@"Start"]; startStopFlag = YES; } } // Here's what the target methods look like. // ViewOutlet is an outlet to a class that responds to "start" and "stop" messages. - (void) start { // Startup the main timer NSTimer *mainTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(theMethodThatWillBeCalledByThisTimer) userInfo:nil repeats:YES]; } - (void) stop { [mainTimer invalidate]; }
The timer will fire until infinity unless the user press the on/off button.

Now that I look at your prior post, I think my code does exactly the same thing. Heck of way to double post!



The only significant differnces relate to garbage collection efforts -which is evident in their code.
(Last edited by DaGuy; Aug 13, 2003 at 08:58 PM. )
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:18 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2