prepareForSegue destination controller property not being set

℡╲_俬逩灬. 提交于 2019-12-02 04:36:45

问题


Here's my prepareForSegue:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqual:@"cameraToRollsSegue"]){
    ALRollsTableViewController *rollsTableViewController = (ALRollsTableViewController *)[segue destinationViewController];
    Camera *c = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
    NSLog(@"CAMERA FROM INSIDE PREPARE FOR SEQUE: %@", c);
    rollsTableViewController.selectedCamera = c;
}

}

I verify that the camera is not null with NSLog:

CAMERA FROM INSIDE PREPARE FOR SEQUE: <Camera: 0x8dc1400> (entity: Camera; id: 0x8dafba0 <x-coredata://A415F856-5F21-4F08-9CAB-4B2A023B55C3/Camera/p1> ;

ALRollsTableViewController viewDidLoad:

- (void)viewDidLoad
{
    NSLog (@"ROLLS TABLE VIEW CONTROLLER : viewDidLoad!");
    NSLog(@"(selected camera = %@", self.selectedCamera);
 }

results in:

ROLLS TABLE VIEW CONTROLLER : viewDidLoad!
(selected camera = (null)

What might I be doing wrong here that the property is not being set?


UPDATE

With matt's help I've determined that the instance of my destination view controller in my prepareForSeque does not match the actual destination view controller:

rollsTableViewController FROM SEGUE: <ALRollViewController: 0x8d90bf0> 
rollsTableViewController FROM viewDidLoad in rollsTableViewController: <ALRollsTableViewController: 0x8c5ab00> 

I don't know why this is the case or what to do to fix it.


回答1:


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.



来源:https://stackoverflow.com/questions/23413884/prepareforsegue-destination-controller-property-not-being-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!