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
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.
精彩评论