简体   繁体   English

创建iOS应用程序时主循环在哪里?

[英]Where does the main-loop go when creating an iOS app?

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) 我在Xcode中为iPhone编写了一个iOS应用程序,我在各自的.h和.m文件中创建了一些类及其方法(这是两个类,所以基本上我有两对.h和.m文件)

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 ? 我是否在ViewController.m中这样做? eg 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. 我在这里读到了类似的问题,有人建议AppDelegate。 Now would that be AppDelegate.m or AppDelegate.h ? 现在是AppDelegate.m还是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? 在Appdelegate文件中?

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 我试图在我创建的game.m文件中简单地开始实例化类并声明泛型方法(不属于任何特定的类...),我尝试初始化元素并不是编译时常量警告实例化任何东西

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.. 来自c ++,它真的可以帮助我一劳永逸地澄清哪个文件准确地编写我的主循环以及是否应该将它包装在某种int main()函数中。

thanks! 谢谢!

PS : 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 为了防止它有任何区别,我的ViewController将只包含一个播放按钮,它会在按下时启动主循环的执行,以及一个终止主循环执行的停止按钮

I have created their respective methods in ViewController.m : 我在ViewController.m中创建了各自的方法:

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

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

which are for the time being empty :) 暂时是空的:)

Every iOS app, as well as every executable file has an entry point - this is the main(). 每个iOS应用程序,以及每个可执行文件都有一个入口点 - 这是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: 您不能拥有多个可执行文件的入口点。如果仔细查看项目,您将看到Xcode导航器中的Supporting Files组中有一个自动生成的main.m文件,如下所示:

#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. 你想做的事情还不够清楚,但是阅读iOS应用程序的结构和生命周期,Objective-c语法,熟悉UIKit以及Apple提供的至少一些框架是一个良好的开端。

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). 你没有在iOS应用程序中使用main (从技术上讲,你确实有一个main,但你不必担心编写它)。 That's all handled for you. 这一切都是为你处理的。 The runloop is all done for you too. runloop也是为你完成的。 All you have to do is create your button and then tell it (via addTarget method) which method to run when it gets pressed. 您所要做的就是创建按钮,然后告诉它(通过addTarget方法)按下时要运行的方法。

Update: This is pseudo(ish) code for what you'd need to do.... 更新:这是你需要做的伪(ish)代码....

[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!) 在你的后台线程中,如果你想更新UI,你可以调用sendMessageOnMainThread (或类似的东西 - 暂时不记得确切的细节!)

The programming methodoly on iOS is different from the C++ methodoly. iOS上的编程方法与C ++方法不同。 In C++ , indeed , you would have to make an infinite loop and get the touches , draw everything , etc at each frame. 实际上,在C ++中,您必须在每个帧上进行无限循环并获取触摸,绘制所有内容等。 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. 在iOS上,情况有所不同:您已经有一个main.m文件,其中有一个main函数。 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. iOS上的编程基于事件。 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. 来自C ++背景肯定是一个良好的开端。

Cheers, 干杯,

George 乔治

EDIT: In that case , I can give more specific info: So , you go from the AppDelegate in your first screen. 编辑:在这种情况下,我可以提供更具体的信息:所以,你从第一个屏幕的AppDelegate。 Let's call it MainAppScreen. 我们称之为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. 其中running是MainAppScreen类中的实例变量。

Hope this helps. 希望这可以帮助。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM