在 Popover tableview 中选择单元格后 UILabel 不刷新

UILabel not refreshing after selecting cell in Popover tableview

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

在 iPad 版本中,如果只是文本,则不能让 tableview 占据整个屏幕。因此,在 iPad 情节提要中,我在主视图控制器上创建了一个按钮,该按钮连接到带有表视图的自定义 ViewController 并且它作为弹出窗口打开。

打开得很好,数据正在从带有表视图的 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]];

        }];
    } 
}

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

-(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);

}

在我看来,而不是

[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];