Search code examples
iosobjective-cuisplitviewcontrolleripadscreen-rotation

replacement of my detailViewController in UISplitView crashes on rotating my app Objective-C


I have a SplitViewController App with 2 initial controllers:

  • leftViewController (master)
  • rightViewController (detail)

my rightViewController (detail) has the delegate of the splitViewController for presenting a button which shows/hides my menu (master)

my leftViewController (master) is a menu application, when I select any element of the menú I trigger a segue connected to my master and it replaces my detailViewController for the selected element of menu

when I do that and if I try to rotate my iPhone for hide menu my app crashes and says:

*** -[rightViewController respondsToSelector:]: message sent to deallocated instance 

I guess it is because my splitViewController wants to communicate with its delegate, its old rightViewController, but it is gone, it has been replaced on my view,

maybe I need whether:

reasign my delegate to my new viewController (detail) or remove delegate of my rightViewController and assign it then to my newViewController

also tried this in my new viewController:

@interface newViewController ()<UISplitViewControllerDelegate>

@end

@implementation newViewController 


- (void)viewDidLoad
{

    UISplitViewController *splitViewController = (UISplitViewController *)self.view.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;
}
...
@end

but still not working... I get the same message

how do I fix this???

thanks in advance

EDIT: add my segue code for helping to answer my question

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"viewSceneAgenda"]) {

        [segue.destinationViewController setTitle:@"Citas"];
        [segue.destinationViewController setUserIDElement:UID_CUS];
        [segue.destinationViewController setOverallAppointments:overallDates];

    }
}

Solution

  • Maybe you have to pass your delegate as you said to your newViewController... why don't you add this in your leftViewController (master)...

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
        if ([segue.identifier isEqualToString:@"showNewViewControllerScene"]) {
    
            UISplitViewController *splitViewController = (UISplitViewController *)self.view.window.rootViewController;
            splitViewController.delegate = segue.destinationViewController;
    
        }
    }
    

    just replace the segue identifier (showNewViewControllerScene) to match with yours on your storyboard