【问题标题】:Updating UI elements in a background thread before being added to a view在添加到视图之前在后台线程中更新 UI 元素
【发布时间】:2012-09-25 19:28:58
【问题描述】:

我只是想了解一下 Grand Central Dispatch (GCD),但我似乎无法找到答案

所以我知道,一般来说,您不应该从主线程以外的任何线程更新 UIView 元素。所以这是非法的:

dispatch_async(workerQueue, ^{
    self.view.backgroundColor = [UIColor redColor];
});

但是您是否允许在视图元素实际添加到视图之前对其进行处理?换句话说,这是否允许?

dispatch_async(workerQueue, ^{
    UIButton *thisLevelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    thisLevelButton.frame = CGRectMake(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
    thisLevelButton.tag = kLevelButtonTags + j;


    int levelState = [[[thisCat objectForKey:[levelNamesInCat objectAtIndex:j]] objectAtIndex:4] intValue];

    [thisLevelButton setBackgroundImage:[UIImage imageNamed:@"ccLSButton50lock"]forState:UIControlStateNormal];
    thisLevelButton.alpha = 0.5;
    [thisLevelButton addTarget:self action:@selector(unlockButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view addSubview:thisLevelButton];
    }

});

【问题讨论】:

    标签: iphone objective-c ios multithreading grand-central-dispatch


    【解决方案1】:

    是的,你可以。最佳做法是不要更新。初始化没问题。

    An example can be read in this blog post. 几乎与您正在尝试的内容一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多