Where does the main-loop go when creating an iOS a

2019-06-23 22:31发布

I am writing an iOS app for the iPhone in Xcode and I have created some classes as well as their methods inside their respective .h and .m files (that's two classes so basically I have two pairs of .h & .m files)

I now I want to start writing my main loop that will be executed whenever the user hits on the play button, but where exactly do I do that ?

Do I do that in ViewController.m ? e.g. 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 in here and someone was suggesting AppDelegate. Now would that be AppDelegate.m or AppDelegate.h ? And if that's the case do I just start writing code or do I include everything inside something like :

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

in the Appdelegate file?

I tried to simply start instantiating classes and declaring generic methods (not belonging to any particular class that is..) in a game.m file I created and I get a initializer element is not a compile-time constant warning as soon as I try instantiating anything

Any help? Coming from c++ it would really help me to clarify once and for all in which file exactly to write my main loop and whether I should wrap it in some kind of an int main() function..

thanks!

PS :

Just in case it makes any difference, my ViewController will only consist of a play button that would start the execution of my main loop whenever its pressed, and a stop button that would terminate the execution of the main loop

I have created their respective methods in ViewController.m :

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

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

which are for the time being empty :)

3条回答
神经病院院长
2楼-- · 2019-06-23 22:54

You don't have a main in iOS apps (well, technically you do have a main, but you don't have to worry about writing it). That's all handled for you. The runloop is all done for you too. All you have to do is create your button and then tell it (via addTarget method) which method to run when it gets pressed.

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

[startButton addTarget:@selector(startPressed:)];
[stopButton addTarget:@selector(stopPressed:)];

-(void)startPressed {
  backgroundThread = [[NSThread alloc] initWithWhateverYouWantToRun];
  [backgroundThread start];
}

-(void)stopPressed {
  [backgroundThread stop];
}

In your background thread, if you want to update the UI, you would call sendMessageOnMainThread (or something similar - can't remember the exact details at the moment!)

查看更多
倾城 Initia
3楼-- · 2019-06-23 22:56

Every iOS app, as well as every executable file has an entry point - this is the main(). You can't have more than one entry points of an executable.And if you look closely into the project you will see that there is an automatically generated main.m file in the Supporting Files group in Xcode's navigator, 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 a good start reading about the structure and the lifecycle of iOS apps, objective-c syntax, get familiar with the UIKit and at least some of the frameworks Apple provide.

查看更多
劫难
4楼-- · 2019-06-23 23:05

The programming methodoly on iOS is different from the C++ methodoly. In C++ , indeed , you would have to make an infinite loop and get the touches , draw everything , etc at each frame. Until the player presses "exit" and you break the loop. On iOS , things are done differently: You already have a main.m file in which you have a main function. That starts up the app delegate. That app delegate tells you when the app finished launching , goes to background , comes in foreground , etc. When the app finished launching , you go to your first actual screen. There , you ADD subviews. You don't draw them at each frame. That is done automatically for you once you have added the view to a parent view. The programming on iOS is based on events. You don't have to check for touches and see if the touch location is on a button and then call the method of that button. Instead , you set a callback method for the button and it's called automatically for you once the button is pressed. Of course , you first need to alloc the button and add it to a parent view.

Once you get used to this event based programming model , you will for sure like it. At the start it may seam very different and maybe it doesn't make sense to you , but don't worry. Comming from a C++ background is surely a good start.

Cheers,

George

EDIT: In that case , I can give more specific info: So , you go from the AppDelegate in your first screen. Let's call it MainAppScreen. Now , you need to add those 2 buttons and set selectors ( callback methods ) for them. I can see you already did that.

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 running is an instance variable in the MainAppScreen class.

Hope this helps.

Cheers!

查看更多
登录 后发表回答