简体   繁体   English

顺序方法执行obj-c

[英]sequential methods execution obj-c

Very beginner obj-c question. 非常初学者的obj-c问题。

My task is to do simple sequence when view loads: 我的任务是在加载视图时执行简单的顺序:

  • pause 2 secons 暂停2秒
  • method1 execution 方法1执行
  • pause 2 seconds 暂停2秒
  • method2 execution method2执行
  • pause 2 seconds 暂停2秒
  • method3 execution method3执行

I write this code for iOS5+ so blocks or other features can be used. 我为iOS5 +编写了此代码,因此可以使用块或其他功能。 I tried 我试过了

[NSThread sleepForTimeInterval:x];

to make the pauses between methods executions, but three pauses added to one and I have one big pause, so it's wrong piece. 在方法执行之间进行停顿,但是三个停顿加了一个,而我有一个大停顿,所以这是错误的。 How to do this in right way? 如何以正确的方式做到这一点?

Sleeping an NSThread is never the right way to handle timed method execution. 休眠NSThread永远不是处理定时方法执行的正确方法。 You want NSTimer , which you can set to either fire the same method after two seconds (then have that method determine which selector to perform next), or have three timers set to fire two seconds after each other, each calling a different method. 您需要使用NSTimer ,您可以将其设置为在两秒钟后触发相同的方法(然后让该方法确定下一个要执行的选择器),或者将三个计时器设置为每隔两秒触发一次,每个计时器调用不同的方法。

You could use performSelector:withObject:afterDelay:, or as CodaFi said, an NSTimer. 您可以使用performSelector:withObject:afterDelay:,或者像CodaFi所说的那样使用NSTimer。 Either method allows to to pass an object to the method, and that object could be an array or dictionary, if you need to pass multiple arguments. 两种方法都可以将一个对象传递给该方法,如果您需要传递多个参数,则该对象可以是数组或字典。

I'd chain the methods: 我会链接方法:

  • call 1. method and make it call 2. 调用1.方法并使其调用2。
    1. method should call 3. 方法应调用3。
    1. should proceed to 4. ... 应该进行到4。

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

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