I am totally new to iOS programming. I am developing an app where I need a different view for portrait and another for landscape orientation. Even if the view is different, the buttons and other controls are the same in both the views. So I want to keep the same connections from the buttons/labels to the view controller for both the views. so that if I change the value of a label in my view controller, it should change in the view irrespective of the view I am presenting.
Now the problem I am facing is that I am not able to add two different root views to an item in the story board. I have tried to add two different sub views to the main view. That way I am able to create the views, but I am not able to connect buttons from both the views to a single connection. For example,
@property (strong, nonatomic) IBOutlet UIButton *buttonNext;
can be connected to a next button in only one view.
Inorder to get around that problem, I have created two different views in the story board, added the same view controller for both. Then created a segue for each view as follows:
@property (strong, nonatomic) IBOutlet UIView *portraitView;
@property (strong, nonatomic) IBOutlet UIView *landscapeView;
And in my view controller I have added:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self adjustViewsForOrientation:toInterfaceOrientation];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
//NSLog(self.view.restorationIdentifier);
if (orientation == UIInterfaceOrientationMaskLandscape || orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
if(self.landscapeView == nil)
{
self.landscapeView =[[UIView alloc] init];
}
self.view = landscapeView;
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = portraitView;
}
}
The problem is that the second view is not loading properly. Initially it is getting loaded as nil. Even when I initialize the view in the code, it is not initializing subviews/controls inside the view. I am stuck with this for the past three days. Any help would be much appreciated.
EDIT 1:
I have added a sample code in here: http://cl.ly/2z3f3E290f0R