0
votes

I have a view that needs to be presented in Landscape and Portrait view both. I used this solution to provide orientation: Solution for orientation in Storyboard

So now my view is like this: enter image description here

The selected view is the View of the ViewController, that has two views: First one being the Portrait View, and Second one being the Landscape view. I hide the view which is not according to the orientation of the device.

As in the image, both the views, Portrait and Landscape have Container views, linked to view controllers through segue. To my understanding, the Storyboard should call all the segues while loading the main view controller. And I am getting the Segue call at prepareForSegue method.

If the Portrait view is kept before the landscape view, prepareForSegue is called first for segues from "Portrait view", where I save the destination viewController in an object. and then for the segues for "Landscape View". But when the Landscape view segues are called, the destination viewController for Portrait Segues become nil (which were saved in a global object).

Why does this happen, and How can I resolve this so that all the container views are loaded properly.

EDIT

prepare for segue method: - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSString * segueName = segue.identifier;

if ([segueName isEqualToString: @"ChartEmbedSegue"])
{
   self.chartViewController = (ChartViewController *) [segue destinationViewController];
   self.chartViewController.dataSource = [[ServiceManager sharedInstance] dataSource];
   self.chartViewController.view.frame = self.chartContainerView.bounds;
}
else if ([segueName isEqualToString: @"LandscapeChartEmbedSegue"])
{
    self.landscapeChartViewController = (ChartViewController *) [segue destinationViewController];
    self.landscapeChartViewController.dataSource = [[ServiceManager sharedInstance] dataSource];
    self.landscapeChartViewController.view.frame = self.chartContainerView.bounds;
}
else if ([segueName isEqualToString: @"SummaryControllerEmbedSegue"])
{
   self.summaryViewController = (SummaryViewController *) [segue destinationViewController];
   self.summaryViewController.data = self.data;
   self.summaryViewController.view.frame = self.summaryView.bounds;
}
else if ([segueName isEqualToString: @"LandscapeSummaryControllerEmbedSegue"])
{
   self.landscapeSummaryViewController = (SummaryViewController *) [segue destinationViewController];
   self.landscapeSummaryViewController.data = self.data;
   self.landscapeSummaryViewController.view.frame = self.landscapeSummaryView.bounds;
}
}
1
Show your prepare for segue. PrepareForSegue is called once for each segue, and if your are setting your global variables or properties in each pass, some of them will become nil based on the order they are called. You can overcome this by lazily setting the pointers.Martin Koles
@MartinKoles Please see the editNikita P
PrepareForSegue you posted looks ok in terms of setting the properties. Where exactly you have the issue with nil destinationViewController?Martin Koles

1 Answers

0
votes

Oops..My Bad, after posting this question, I realized, I was not setting an important property, and hence there was no data coming.

Sometimes, just rethinking the problem solves it.

Moderators: Should I remove the question?

@MartinKoles Thanks for your support.