Where does the main loop go when creating an iOS app?

I am writing an iOS application for iPhone in Xcode, and I created several classes, as well as their methods in my .h and .m files (which are two classes, so basically I have two pairs of .h and .m files)

Now I want to start writing my main loop , which will be executed whenever the user clicks the play button, but where exactly am I doing this?

Am I doing this in ViewController.m? for example inside this method:

- (IBAction)playPressed:(UIButton *)sender { // main loop executed in here ? // or simply message to the main loop to start executing is *sent* from here ? } 

I read about a similar question here, and someone suggested AppDelegate. Now will it be AppDelegate.m or AppDelegate.h? And if so, I'm just starting to write code or include everything inside:

 int main(int argc, char **argv) { .... } 

in the Appdelegate file?

I tried to just start an instance of classes and declare common methods (not belonging to any particular class that is ..) in the game.m file that I created and I get the initialization element is not a compile time constant as soon as I try create something.

Any help? Based on C ++, this would really help me figure out once and for all in which file my main loop should write, and should I wrap it in some kind of int main () function ..

thanks!

PS:

Just in case, it matters, my ViewController will consist only of a play button that starts the execution of my main loop whenever it is pressed, and a stop button that stops the main loop

I created my respective methods in ViewController.m:

 - (IBAction)playPressed:(UIButton *)sender { // } - (IBAction)stopPressed:(UIButton *)sender { // ?? } 

which are still empty :)

+4
source share
3 answers

The programming method on iOS is different from the C ++ method. In C ++, indeed, you will have to create an endless loop and get strokes, draw everything, etc. In every frame. Until the player presses "exit" and you interrupt the cycle. On iOS, everything is done differently: You already have the main.m file, in which you have the main function. This will launch the application delegate. This application delegate tells you when the application finishes launching, goes to the background, appears in the foreground, etc. When the application finishes launching, you will be taken to your first actual screen. There are ADD subviews there. You do not draw them in every frame. This is done automatically for you as soon as you add the view to the parent view. IOS programming is event driven. You do not need to check for touches and click on the button, and then call the method of this button. Instead, you set a callback method for the button, and it is automatically called after you click the button. Of course, you first need to select the button and add it to the parent view.

Once you get used to this event-based programming model, you will surely like it. This can be very different at first, and it may not make sense to you, but don't worry. Starting with a C ++ background is definitely a good start.

Greetings

George

EDIT: In this case, I can provide more detailed information: So, you are moving from AppDelegate to your first screen. Let me call it MainAppScreen. Now you need to add these 2 buttons and set a selector for them (callback methods). I see you already did it.

Now:

 - (IBAction)playPressed:(UIButton *)sender { running = TRUE; [self performSelectorInBackground:@selector(myLoop) withObject:nil]; } - (IBAction)stopPressed:(UIButton *)sender { running = FALSE; } - (void) myLoop { while(running) { //this is your loop. You can code in here. } } 

Where the instance variable is executed in the MainAppScreen class.

Hope this helps.

Hooray!

+1
source

Each iOS application, as well as each executable file, has an entry point - this is main (). You cannot have more than one executable entry point. If you look closely at the project, you will see that in the Xcode navigator there is a file automatically generated by main.m in the Supporting Files group, which looks like this:

 #import <UIKit/UIKit.h> #import "MyAppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class])); } } 

What you want to do is not clear enough, but it's nice to start reading about the structure and life cycle of iOS applications, the objective-c syntax, get to UIKit , and at least some of the Apple frameworks ..

+1
source

You do not have the main application in iOS applications (well, technically you have the main thing, but you do not need to worry about writing it). This is all for you. Hall is all done for you too. All you have to do is create your own button and then pass it (using the addTarget method), which method to run when clicked.

Update: This is pseudo (ish) code for what you need to do ....

 [startButton addTarget:@selector(startPressed:)]; [stopButton addTarget:@selector(stopPressed:)]; -(void)startPressed { backgroundThread = [[NSThread alloc] initWithWhateverYouWantToRun]; [backgroundThread start]; } -(void)stopPressed { [backgroundThread stop]; } 

In the background thread, if you want to update the user interface, you would call sendMessageOnMainThread (or something like that - you could not remember the exact data at the moment!)

+1
source

All Articles