在Popover tableview中选择单元格后,UILabel没有刷新

时间:2017-12-11 23:37:20

标签: ios objective-c uitableview uilabel popover

Ok所以在IB中,我创建了一个带有iPhone版桌面视图的ViewController,效果很好。

在iPad版本上,如果只是文本,你很可能没有一个tableview占据整个屏幕。因此,在iPad故事板中,我在主视图控制器上创建了一个按钮,该按钮连接到具有tableview的自定义ViewController,而是作为弹出窗口打开。

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

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

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

以下是在弹出窗口中选择单元格时,如何使用tableview解除视图控制器。

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

我解雇它是否阻止主视图控制器更新?

这是popover中的方法:

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

}

1 个答案:

答案 0 :(得分: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];