views:

67

answers:

1
+1  Q: 

objective c loops

How can I loop right in the middle of objective c? For example in the .m file:

#import "untitled.h"


@implementation untitled

- (IBAction) runSample:(id)sender {
(this is running when the button is pressed)
}

(Something here that just constantly repeat that won't mess anything else up)

@end

Any ideas??

Elijah

+4  A: 

Short answer; don't. You don't ever want to loop actively in a Mac OS X or iPhone application. It burns CPU, makes the UI unresponsive, and is otherwise very inefficient. And you really don't want to block the main event loop on either platform; on Mac OS X it leads to the rainbow cursor while on the iPhone, your app will be killed.

Even Modal Event Loops aren't really active loops; they are a way to grossly limit the kind and scope of events processed by your app.

The best thing you can do is set yourself up in the run loop to receive whatever events you need. You can also use timers, if you need, but -- again -- polling is to be actively avoided.

The Threaded Programming Guide provides information about run loops and otherwise doing processing such that you don't block the main event loop.

bbum
Ok, really quick:I need to check if a value is less than 5 and if it's more than 5 continuously when the application runs. How would I do that?
Elijah W.
Given the complete lack of information about who/what triggers the change or the required latency, hard to say. But, in general, trigger a check when the value changes.
bbum
Oh using controlTextDidChange:?
Elijah W.
User input? Sure, that'd work. So would an action method.
bbum
No, a text label. Another part of my code changes the value many times a second. Can you show an example of controlTextDidChange:?
Elijah W.
Well why don't you check for the value in the part of your code that sets it?
Jonathan Grynspan