0

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

snowcold
  • 71
  • 6

2 Answers2

3

U can try this with single view :

First

In viewDidLoad method, code for Device Orientation

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

Second

Method for OrientationChanged

-(void)orientationChanged {

    //Check for Portrait Mode
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
       x=25; y=30;
    }
    //Lanscape mode
    else {
       x=50; y=40;
    }

    [button setFrame:CGRectMake(x, y, 80, 80)];
}

Here, you can take 'X' & 'Y' whatever you want.

This method always calls whenever device will be rotated.

Hopefully, it will help you.

Thanks.

Manann Sseth
  • 2,745
  • 2
  • 30
  • 50
  • Thanks for the reply. But I am looking at using two different views rather than resizing/re positioning the objects. Otherwise it will be very tedious for me to make this change in every screen because it is a rich UI. – snowcold Mar 11 '13 at 08:27
  • But, as you said earlier that you have to use same object(i.e. button), then how will you manage with multiple connection for same object. Then you need to resize controls. – Manann Sseth Mar 11 '13 at 08:34
0

Try this

 UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
        if (newOrientation != UIDeviceOrientationUnknown && newOrientation != UIDeviceOrientationFaceUp && newOrientation != UIDeviceOrientationFaceDown)
        {
            if(orientation != newOrientation)
            {
                //ORIENTATION HAS CHANGED. Do orientation logic
                if(
                   (newOrientation == UIDeviceOrientationLandscapeLeft || newOrientation == UIDeviceOrientationLandscapeRight)&&(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown))
                {
                    NSLog(@"Changed orientation to Landscape");
                    // Clear the current view and insert the orientation-specific view
                    [self clearCurrentView];
                    [self.view insertSubview:titleLandscapeView atIndex:0];
                }
                else if (
                         (newOrientation == UIDeviceOrientationPortrait || newOrientation == UIDeviceOrientationPortraitUpsideDown)&&(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight))
                {
                    NSLog(@"Changed orientation to Portrait");
                    // Clear the current view and insert the orientation-specific view
                    [self clearCurrentView];
                    [self.view insertSubview:titlePortraitView atIndex:0];
                }
                orientation = newOrientation;
            }

 -(void) clearCurrentView{
        if (titleLandscapeView.superview){
            NSLog(@"clear titleLandscapeView");
            [titleLandscapeView removeFromSuperview];
        } else if (titlePortraitView.superview){
            NSLog(@"clear titlePortraitView");
            [titlePortraitView removeFromSuperview];
        } else
        {
            NSLog(@"Neither titleLandscapeView nor titlePortraitView");
        }
    }
Dhara
  • 4,093
  • 2
  • 36
  • 69