gpt4 book ai didi

iphone - 执行 segue 后,MBProgressHUD 事件指示器不显示在 viewDidAppear 中

转载 作者:行者123 更新时间:2023-12-01 16:48:50 26 4
gpt4 key购买 nike

我有两个 UITableViewControllers A和B,当我点击A中的表格 View 单元格时,这就是我想要做的:

  • 通过从 A 设置一些 B 的变量,准备从 A 到 B。
  • 执行从 A 到 B 的 segue。
  • B 出现。
  • 使用 [MBProgressHUD][1] 显示“加载”事件指示器.
  • 在后台任务中,从 URL 检索数据。
  • 如果 URL 请求中发生错误(未收到数据或非 200 状态代码),(a) 隐藏事件指示器,然后 (b) 显示带有错误消息的 UIAlertView
  • 否则,(a) 重新加载 B 的 tableView使用检索到的数据,然后 (b) 隐藏事件指示器

  • 但是,这就是正在发生的事情,我不知道如何解决它:
  • 单击 A 中的单元格后,B 从右侧滑入,空白处为 UITableView . MBProgressHUD 不显示 .
  • 过了一会儿,tableView使用检索到的数据重新加载 MBProgressHUD出现得很短暂。
  • MBProgressHUD立即消失。

  • 执行后台任务的方式似乎没有错误。我的问题是,如何显示 MBProgressHUD我的 B View Controller 出现后的事件指示器? (实际上,为什么它没有显示?)代码如下。
    A的prepareForSegue
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    B *b = (B *)[segue destinationViewController];

    // Set some of B's variables here...
    }
    B中的相关方法
    - (void)viewDidAppear:(BOOL)animated {
    [self startOver];
    }

    - (void)startOver {
    [self displayLoadingAndDisableTableViewInteractions];
    [self retrieveListings];
    [self.tableView reloadData];
    [self hideLoadingAndEnableTableViewInteractions];
    }

    - (void)displayLoadingAndDisableTableViewInteractions {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    self.tableView.userInteractionEnabled = NO;
    }

    - (void)hideLoadingAndEnableTableViewInteractions {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    self.tableView.userInteractionEnabled = YES;
    }

    - (void)retrieveListings {
    __block NSArray *newSearchResults;

    // Perform synchronous URL request in another thread.
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    newSearchResults = [self fetchNewSearchResults];
    });

    // If nil was returned, there must have been some error--display a UIAlertView.
    if (newSearchResults == nil) {
    [[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"An unknown error occurred. Try again later?" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    } else {
    // Add the retrieved data to this UITableView's model. Then,
    [self.tableView reloadData];
    }
    }

    - (NSArray *)fetchNewSearchResults {
    // Assemble NSMutableArray called newSearchResults from NSURLConnection data.
    // Return nil if an error or a non-200 response code occurred.
    return newSearchResults;
    }

    最佳答案

    我想你必须调用[self hideLoadingAndEnableTableViewInteractions];newSearchResults = [self fetchNewSearchResults]; 之后您正在另一个线程中检索数据,这意味着 -startOver调用 [self retrieveListings]; 后将继续执行并将立即隐藏 HUD。此外,因为您正在更新显示,您必须确保您在主线程上执行此操作。查看示例

    dispatch_async(dispatch_get_main_queue(), ^{
    //update UI here
    });

    关于iphone - 执行 segue 后,MBProgressHUD 事件指示器不显示在 viewDidAppear 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17824770/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com