prepareForSegue destination controller property not being set

后端 未结 1 1194
刺人心
刺人心 2021-01-21 14:46

Here\'s my prepareForSegue:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqual:@\"cameraToRollsS         


        
1条回答
  •  长发绾君心
    2021-01-21 15:12

    Post-chat summary:

    Well, it was complicated! But basically you were saying this:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqual:@"cameraToRollsSegue"]){
        ALRollsTableViewController *rollsTableViewController = (ALRollsTableViewController *)[segue destinationViewController];
        // ...
    }
    

    The problem was that [segue destinationViewController] was not an ALRollsTableViewController. Thus you were not talking to the instance you thought you were talking to, and you were not talking to an instance of the class you thought you were talking to.

    The amazing thing is that your code didn't crash when it ran. You were saying this:

    rollsTableViewController.selectedCamera = c;
    

    But rollsTableViewController was not in fact an ALRollsTableViewController. You lied to the compiler when you cast incorrectly. Yet you didn't crash when that line ran. Why not? It's because you've got lots of classes with @property selectedCamera! So you were setting the property of a different class. But a property with that same name did exist in that class, so you didn't crash. Thus you didn't discover that this was the wrong class and the wrong instance.

    0 讨论(0)
提交回复
热议问题