目标C-无法在主线程上更新UI [英] Objective C- Trouble updating UI on main thread

查看:108
本文介绍了目标C-无法在主线程上更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用performSelectorOnMainThread更新我的UI时遇到了一些麻烦.这是我的情况.在我的viewDidLoad中,我设置了一个活动指示器和一个标签.然后,我调用选择器以从服务器检索一些数据.然后,我调用一个选择器以在延迟后更新UI.这是代码:

I am having some trouble updating my UI using performSelectorOnMainThread. Here is my situation. In my viewDidLoad I set up an activity indicator and a label. Then I call a selector to retrieve some data from a server. Then I call a selector to update the UI after a delay. Here's the code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.reloadSchools = [[UIAlertView alloc] init];
    self.reloadSchools.message = @"There was an error loading the schools. Please try again.";
    self.reloadSchools.title = @"We're Sorry";
    self.schoolPickerLabel = [[UILabel alloc]init];
    self.schoolPicker = [[UIPickerView alloc] init];
    self.schoolPicker.delegate = self;
    self.schoolPicker.dataSource = self;
    self.server = [[Server alloc]init];
    schoolList = NO;

    _activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:_activityIndicator];
    [self.view bringSubviewToFront:_activityIndicator];
    [_activityIndicator startAnimating];

    [NSThread detachNewThreadSelector: @selector(getSchoolList) toTarget: self withObject: nil];
    [self performSelector:@selector(updateUI) withObject:nil afterDelay:20.0];
}

选择器updateUI检查是否已检索到数据,并在主线程上调用选择器以相应地更新UI.这是这些部分的代码:

The selector updateUI checks to see if the data was retrieved, and calls a selector on the main thread to update the UI accordingly. Here is the code for these parts:

-(void)updateUI
{
    self.schools = [_server returnData];
    if(!(self.schools == nil)) {
        [self performSelectorOnMainThread:@selector(fillPickerView) withObject:nil waitUntilDone:YES];
    }
    else {
        [self performSelectorOnMainThread:@selector(showError) withObject:nil waitUntilDone:YES];
    }
}

-(void)showError {
    NSLog(@"show error");
    [_activityIndicator stopAnimating];
    [self.reloadSchools show];
    }
-(void)fillPickerView {
     NSLog(@"fill picker view");
     schoolList = YES;
     NSString *schoolString = [[NSString alloc] initWithData:self.schools encoding:NSUTF8StringEncoding];
     self.schoolPickerLabel.text  = @"Please select your school:";
     self.shoolArray = [[schoolString componentsSeparatedByString:@"#"] mutableCopy];
     [self.schoolPicker reloadAllComponents];
     [_activityIndicator stopAnimating];
}

调用选择器fillPickerView时,活动指示器持续旋转,标签文本不会更改,并且选择器视图不会重新加载其内容.有人可以向我解释为什么我使用的方法无法在主线程上更新我的ui吗?

When the selector fillPickerView is called the activity indicator keeps spinning, the label text doesn't change, and the picker view doesn't reload its content. Can someone explain to me why the method I am using isn't working to update my ui on the main thread?

推荐答案

dispatch_async(dispatch_get_global_queue(0, 0), ^{
 //load your data here.
 dispatch_async(dispatch_get_main_queue(), ^{
                //update UI in main thread.
            });
});

这篇关于目标C-无法在主线程上更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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