UIView#addSubview and threading

Discussion in 'iOS Programming' started by Runnable, Mar 20, 2009.

  1. Runnable macrumors newbie

    Joined:
    Aug 20, 2008
    Location:
    BadenWürtemberg, Germany
    #1
    Hi @all,

    i just wanted to add a subview to my main window from another thread.
    like so:

    Code:
    AppDelegate.m
    ...
    UpdateThread *t = [[UpdateThread alloc]init];
    [t setWindow:window];
    [t start];
    ...
    Code:
    UpdateThread#main
    ...
    UILabel *l = [[UILabel alloc]initWithFrame:...];
    //label's setup
    [window addSubview:label];
    ...
    Now i have the problem, that it does not work, there's no label visible after the thread terminates.
    If i run the main's code in the Application's thread that calls applicationDidFinishLaunching it works properly.

    can anyone please help me ?
     
  2. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    AFAIK you should do UI updates on the main thread.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    You can't access UI code from a second thread like this. You can use performSelectorOnMainThread from your secondary thread to make the update occur on the main thread.
     
  4. Runnable thread starter macrumors newbie

    Joined:
    Aug 20, 2008
    Location:
    BadenWürtemberg, Germany
    #4
    yeah, thank u PhoneyDeveloper,
    that's the way it works
     

Share This Page