3
votes

While doing some tests on my old iPhone 4 (iOS 7.1.2) I encountered an error performing a segue from my initial collectionView to a tableView.

Everything works fine on iOS 8, but I get the following error on iOS 7:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ListaPlatosTableViewController topViewController]: unrecognized selector sent to instance 0x17df3c70'

My prepareForSegue method is the following:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

UINavigationController *nav = [segue destinationViewController];
ListaPlatosTableViewController *listaPlatosVC = (ListaPlatosTableViewController *)nav.topViewController;
[listaPlatosVC setPlatosCarta:platosCarta];

//I have set selectedCell in the method where I call performSegueWithIdentifier
[[[segue destinationViewController]topViewController]setTitle:selectedCell];

}

//UPDATE: Here's the code that calls the segue, in case I'm overlooking something

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
selectedCell = [tiposCarta objectAtIndex:indexPath.row];
[self ponerPlatosEnCarta]; // I prepare platosCarta with this method
[self performSegueWithIdentifier:@"ListaPlatos" sender:self];
}

has anyone encountered this?

2
iOS7 & 8 are both using the same storyboard, and this is for exactly the same test (so the same segue)? - Wain
Can you check if nav is type of ListaPlatosTableViewController. I think it is a UINavigationController, or ListaPlatosTableViewController doesn't have a property called topViewController. - Mustafa Ibrahim
@Wain - I'm testing the exact same code on both devices, selecting the same collection view cell and it works on one while failing on the other... - Ockie
topViewController is a property of a navigation controller, not a table view controller, so the segue should point to a navigation controller for your code to work. If the same storyboard is used then the code should work or fail in both cases, you need to debug in both cases to see what's happening. - Wain
topViewController is a property to any subclass of UINavigationController only. So you cannot use it with UITableViewController - Mustafa Ibrahim

2 Answers

2
votes

topViewController is a property of UINavigationController.

here's the problem:enter image description here Then have a segue to ListaPlatosTableViewController. Your code should look like:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    ListaPlatosTableViewController *listaPlatosVC = (ListaPlatosTableViewController *)segue.destinationViewController;

    listaPlatosVC.platosCarta = platosCarta;
    listaPlatosVC.title = selectedCell;
}
0
votes

My solution: Perform the segues programmatically, not using the control-click in storyboard. I don't know if I did something wrong with storyboard or if it is a bug with Xcode-ios7 (as it works fine for iOS8) Any how, what i did was:

Disconnect the views that were giving problems in Storyboard, and prepare + launch them from the following methods:

my custom tableview launched from the collectionView:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
selectedCell = [tiposCarta objectAtIndex:indexPath.row];
[self ponerPlatosEnCarta];
ListaPlatosTableViewController *listaPlatosVC = [self.storyboard instantiateViewControllerWithIdentifier:@"listaPlatosVC"];
[self.navigationController pushViewController:listaPlatosVC animated:YES];
[listaPlatosVC setPlatosCarta:platosCarta];
[listaPlatosVC setTitle:selectedCell];
}

the "detail" view launched from the tableView:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 PlatoHormigueroViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
 dvc.elPlato = [self.platosCarta objectAtIndex:indexPath.row];
 [tableView deselectRowAtIndexPath:indexPath animated:NO];
 [self.navigationController pushViewController:dvc animated:YES];

}

This way it works the same on iOS 7 as 8.

Thanks everyone who replied in this thread for your time! :)