【问题标题】:Pushing view controller within block not working在块内推送视图控制器不起作用
【发布时间】:2014-11-17 02:22:42
【问题描述】:

在完成块中触发方法的正确方法是什么(如果甚至建议这样做)?现在,我有一个 IBAction,它调用一个下载信息的方法,该方法带有一个完成块,表示信息是否已成功检索。如果是这样,我想推送一个显示该信息的视图控制器,但目前什么都没有发生。我猜它与主线程、gcd 等有关...

__weak YTTMSetupViewController *weakSelf = self;
    [mc downloadJson:^(BOOL success) {
            if(success){
                NSLog(@"sucess. metric count - %i",(int)mc.collection.count);

                //info was downloaded. Push new view controller with info
                YTTMMetricTableViewController *mtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YTTMMetricTableViewController"];
                mtvc.group = (WAGroup*)[[WAMetricCollection sharedInstance].collection lastObject];
                mtvc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:mtvc animated:YES];
            }
            else{
                NSLog(@"failure");
                //display failure UI
            }
            NSLog(@"end of downloading");
            [HUD dismissAfterDelay:0.5f animated:YES];
        }];

【问题讨论】:

  • 您可以尝试从 dispatch_asynch 调用 pushViewController,延迟 0.0 秒。通常,它可以工作。

标签: ios objective-c xcode objective-c-blocks


【解决方案1】:

不确定这是否是正确的方法,但它确实有效。

我添加了一个将 vc 推送到主线程的方法:

        [weakSelf performSelectorOnMainThread:@selector(pushDetail) withObject:nil waitUntilDone:YES];

完成的代码:

__weak YTTMSetupViewController *weakSelf = self;
    [mc downloadJson:^(BOOL success) {
            if(success){
                NSLog(@"sucess. metric count - %i",(int)mc.collection.count);

                //info was downloaded. Push new view controller with info
                [weakSelf performSelectorOnMainThread:@selector(pushDetail) withObject:nil waitUntilDone:YES];
            }
            else{
                NSLog(@"failure");
                //display failure UI
            }
            NSLog(@"end of downloading");            
        }];

}

-(void)pushDetail{
    __weak YTTMSetupViewController *weakSelf = self;
    YTTMMetricTableViewController *mtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YTTMMetricTableViewController"];
    mtvc.group = (WAGroup*)[[WAMetricCollection sharedInstance].collection lastObject];
    mtvc.hidesBottomBarWhenPushed = YES;
    [weakSelf.navigationController pushViewController:mtvc animated:YES];

}

【讨论】:

    【解决方案2】:

    您可以简单地尝试使用 dispatch_asynch 块包装调用...

    __weak YTTMSetupViewController *weakSelf = self;
        [mc downloadJson:^(BOOL success) {
            if(success){
                NSLog(@"sucess. metric count - %i",(int)mc.collection.count);
    
                dispatch_async(dispatch_get_main_queue(), ^{
                    //info was downloaded. Push new view controller with info
                    YTTMMetricTableViewController *mtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YTTMMetricTableViewController"];
                    mtvc.group = (WAGroup*)[[WAMetricCollection sharedInstance].collection lastObject];
                    mtvc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:mtvc animated:YES];
                });
            }
            else{
                NSLog(@"failure");
                //display failure UI
            }
            NSLog(@"end of downloading");
            [HUD dismissAfterDelay:0.5f animated:YES];
        }];
    

    【讨论】:

      【解决方案3】:

      所有 UI 更新都必须在主线程上执行。就我个人而言,我更喜欢通过GCD 执行此操作,因为它产生的代码比performSelectorOnMainThread 更具可读性。但是,除了个人偏好之外,performSelectorOnMainThread 在执行某些完成块之后在主线程上调用单个 UI 更新的情况下没有任何问题。请注意,无论您选择哪一个,都应该be consistent with what you use to guarantee that blocks are enqueued in the order you specified

      然而,除了工作代码之外,Apple 的框架似乎使用的约定是在主线程上执行所有完成块,除非将队列指定为方法参数,在这种情况下,应该在该队列上执行完成块。因此,在这种情况下,我建议您编辑下载处理程序类的 downloadJson 方法,以自动在主队列上执行完成块。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-05
        • 2020-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多