创建iOS应用时,主循环到哪里去了? [英] Where does the main-loop go when creating an iOS app?

查看:94
本文介绍了创建iOS应用时,主循环到哪里去了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Xcode编写用于iPhone的iOS应用,并且已经在各自的.h和.m文件中创建了一些类及其方法(这是两个类,所以基本上我有两对.h&. m个文件)

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 ?

我可以在ViewController.m中执行此操作吗?例如在此方法内:

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 ?
}

我在这里读到一个类似的问题,有人建议使用AppDelegate.现在是AppDelegate.m或AppDelegate.h吗?如果是这种情况,我应该只是开始编写代码还是将所有内容都包含在这样的内容中:

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)
{
   ....
}

在Appdelegate文件中?

in the Appdelegate file?

我试图简单地开始实例化类,并在我创建的game.m文件中声明泛型方法(不属于任何特定类.),我得到一个 initializer元素不是编译器时间常数 ,当我尝试实例化任何内容时会发出警告

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

有帮助吗?来自c ++,它确实可以帮助我一劳永逸地澄清要在哪个文件中确切地写出我的主循环,以及是否应该将它包装在某种int main()函数中.

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..

谢谢!

PS:

以防万一,我的ViewController将只包含一个播放按钮(无论何时按下它,都会开始执行主循环)和一个停止按钮(会终止执行主循环)

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.m中创建了它们各自的方法:

I have created their respective methods in ViewController.m :

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

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

暂时为空:)

推荐答案

iOS上的编程方法与C ++方法不同. 实际上,在C ++中,您将必须进行无限循环并在每一帧进行触摸,绘制所有内容等. 直到玩家按下退出"按钮,您才中断循环. 在iOS上,处理方式有所不同: 您已经有一个main.m文件,在其中您具有一个主要功能. 这将启动应用程序委托.该应用程序代表会告诉您应用程序何时启动,进入后台,进入前台等. 应用启动完成后,您将进入第一个实际屏幕. 在这里,您可以添加子视图.您不会在每一帧都绘制它们.将视图添加到父视图后,该操作将自动为您完成. iOS上的编程基于事件.您无需检查触摸是否正常 触摸位置在按钮上,然后调用该按钮的方法. 相反,您可以为按钮设置一个回调方法,并在按下按钮后自动为您调用该方法. 当然,您首先需要分配按钮并将其添加到父视图.

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.

一旦您习惯了这种基于事件的编程模型,您肯定会喜欢它. 刚开始时,接缝可能有很大不同,也许对您来说没有意义,但请不要担心. 来自C ++的背景肯定是一个很好的开始.

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.

干杯

乔治

在这种情况下,我可以提供更多具体信息: 因此,您从第一个屏幕中的AppDelegate进入.我们称它为MainAppScreen. 现在,您需要添加这两个按钮并为其设置选择器(回调方法).我可以看到你已经做到了.

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.

现在:

- (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.
    }
}

其中运行的是MainAppScreen类中的一个实例变量.

Where running is an instance variable in the MainAppScreen class.

希望这会有所帮助.

干杯!

这篇关于创建iOS应用时,主循环到哪里去了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆