【问题标题】:UILabel not refreshing after selecting cell in Popover tableview在 Popover tableview 中选择单元格后,UILabel 不刷新
【发布时间】:2018-05-25 13:58:12
【问题描述】:

好的,所以在 IB 中,我为 iPhone 版本创建了一个带有 tableview 的 ViewController,效果很好。

在 iPad 版本上,如果它只是文本,你就不能让 tableview 占据整个屏幕。因此,在 iPad Storyboard 中,我在主视图控制器上创建了一个按钮,该按钮通过 tableview 连接到自定义 ViewController,并以弹出窗口的形式打开。

打开得很好,数据正在从带有 tableview 的 ViewController 传递到主视图。在控制台中,我看到标签正在更新,但在屏幕上标签没有更新。

在我用来检索数据的方法中尝试了[self.view layoutIfNeeded],按照某些人在其他答案中的建议更改了 alpha。我注意到 viewWillAppear 根本没有被调用。尝试将 didSelectRowAtIndex 中的 main vc viewWillAppear 设置为 yes...

当我转到另一个屏幕并返回时,标签当然会更新。

这是我在选择弹出窗口中的单元格时使用 tableview 关闭视图控制器的方式。

[self dismissViewControllerAnimated:YES completion:^{
    [vc viewWillAppear:YES];
    [vc.view layoutIfNeeded];
    [vc updateText:[arrUsers objectAtIndex:indexPath.row]];
}];

是不是我将其解除阻止了主视图控制器的更新?

这是弹出框里的方法:

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];


UIStoryboard * storyboard = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ?
[UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil] :
[UIStoryboard storyboardWithName:@"Main" bundle:nil];

ViewController *vc = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ?
(ViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ViewControlleriPad"] :
(ViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{        
    if(searching){
        if ([copyListOfItems count]>0) {

            NSLog(@"Search Results Selected %@", [copyListOfItems objectAtIndex:indexPath.row]);

            [vc.lbl_specialties setText:[copyListOfItems objectAtIndex:indexPath.row]];
        }
    }
    else {

        self.appDelegate.selectedSpecialty = [arrUsers objectAtIndex:indexPath.row];

        NSLog(@"Selected %@", self.appDelegate.selectedSpecialty);


    }
        [self dismissViewControllerAnimated:YES completion:^{
            [vc viewWillAppear:YES];
            [vc.view layoutIfNeeded];
            [vc updateSpecialtyText:[arrUsers objectAtIndex:indexPath.row]];

        }];
    } 
}

这是上面调用的 View 控制器中的方法。

-(void)updateSpecialtyText:(NSString*)text
{
    [super viewWillAppear:YES];

    NSLog(@"text to update %@", text);
    [self.lbl_specialties layoutIfNeeded];
    [self.view layoutIfNeeded];
    [self.lbl_specialties setText:text];

    NSLog(@"text updated %@", self.lbl_specialties.text);

}

【问题讨论】:

  • 你能说明你从哪里得到vc值吗?
  • @trungduc 我实例化了主 ViewController。 ViewController *vc = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? (ViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ViewControlleriPad"] : (ViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
  • 请显示方法的完整代码你在哪里调用它
  • @trungduc 编辑的问题,因为它在 cmets 中太混乱了
  • 好的,我明白了。还有一个问题。据我了解,当前视图控制器由ViewController 呈现,现在您想要关闭当前控制器并更新ViewController。对吗?

标签: ios objective-c uitableview uilabel popover


【解决方案1】:

在我看来,而不是

[self dismissViewControllerAnimated:YES completion:^{
    [vc viewWillAppear:YES];
    [vc.view layoutIfNeeded];
    [vc updateText:[arrUsers objectAtIndex:indexPath.row]];
}];

你应该试试

[(ViewController *)self.presentingViewController updateText:[arrUsers objectAtIndex:indexPath.row]];
[self dismissViewControllerAnimated:YES completion:nil];

如果ViewController 嵌入在UINavigationController 中,请改用以下代码。

[(ViewController *)[(UINavigationController *)self.presentingViewController topViewController] updateText:[arrUsers objectAtIndex:indexPath.row]];
[self dismissViewControllerAnimated:YES completion:nil];

【讨论】:

  • 我收到*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController updateSpecialtyText:]: unrecognized selector sent to instance 0x152829600'
  • 显示ViewController 中的代码,用于呈现当前视图控制器
  • 我是在 Storyboard 中完成的。我在 ViewController 中放置了一个按钮,选择它控制将其拖到 SpecialtyViewControler 并选择Present As Popover
  • @GunplaGamer 更新了我的答案,你可以再看看
  • Woooooooo :D 做到了 :D 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多