简体   繁体   中英

UILabel not refreshing after selecting cell in Popover tableview

Ok So in IB I created a ViewController with a tableview for the iPhone version and works great.

On the iPad version you very well can't have a tableview take up the entire screen if its just text. So in the iPad Storyboard I created a button on the main view controller that is connected to the custom ViewController with a tableview and it opens as a popover instead.

Opens up great, data is being passed from the ViewController with the tableview to the main one. In the console, I am seeing the label is being updated, but on screen the label doesn't update.

Tried [self.view layoutIfNeeded] in the method that I used to retrieve the data, changing the alpha as some people suggested in other answers. I noticed the viewWillAppear does not get called at all. Tried setting main vc viewWillAppear in the didSelectRowAtIndex to yes...

The label gets updated of course when I go to another screen and come back.

Here is how I am dismissing the view controller with a tableview upon selecting a cell in the popover.

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

Is it the fact that I am dismissing it preventing the main view controller from updating?

This is the method in the 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]];

        }];
    } 
}

This is the method in the View controller that is called above.

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

}

In my opinion, instead of

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

you should try

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

If ViewController is embedded in an UINavigationController , use the code below instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM