How do you make apps wait a certain number of seconds?

Discussion in 'iOS Development' started by Keeheon, Apr 7, 2009.

  1. Keeheon

    Keeheon Member

    Joined:
    Mar 9, 2008
    Messages:
    171
    Likes Received:
    0
    Device:
    iPhone 4S (Black)
    How do you make apps wait a certain number of seconds? I want it to wait like 5-10 seconds... Thanks
  2. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    you could stop anything you're doing, and then resume on the action selector of an nstimer, or you could try sleep() which takes a parameter that is x seconds.
  3. Meikura

    Meikura Super Moderator Staff Member

    Joined:
    Jan 16, 2008
    Messages:
    6,081
    Likes Received:
    431
    Device:
    iPhone 4 (Black)
    Thread title updated. Make sure you use more helpful thread titles, or you won't get any help.
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    sleep(int seconds) works just fine. I don't like to use it though, as it will cause the app to sleep for however long. I prefer to use NSThread to freeze whichever thread needs to sleep. That way I can continue doing things while waiting.

    Also, if you're simply trying to wait until you're done doing something, then you'd be best off to use the following method. It is a method of NSObject, so you can use it on any class. Using it will cause whatever thread you're working on to wait until the selector mentioned has completed.
    Code:
    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
    //OR
    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait
  5. Keeheon

    Keeheon Member

    Joined:
    Mar 9, 2008
    Messages:
    171
    Likes Received:
    0
    Device:
    iPhone 4S (Black)
    How do i do the sleep() thing? Where do I put that code and the number of seconds?
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    God, I hate the sleep() solution. If you *must,* use it, just put it at the point you want the sleeping to occur. And feed it the number if seconds in an int.
  7. Keeheon

    Keeheon Member

    Joined:
    Mar 9, 2008
    Messages:
    171
    Likes Received:
    0
    Device:
    iPhone 4S (Black)
    XD thanks!
  8. Keeheon

    Keeheon Member

    Joined:
    Mar 9, 2008
    Messages:
    171
    Likes Received:
    0
    Device:
    iPhone 4S (Black)
    still having troubles... Skylar, would you mind if you could help me for free? I saw your other help posts saying you have a $100/hour fee... I really dont have much money... please?
  9. e3m

    e3m New Member

    Joined:
    Sep 29, 2007
    Messages:
    8
    Likes Received:
    0
    Code:
    - (void)viewDidLoad {
    sleep(5);
    }

Share This Page