Trying to load a new orientation change

I am trying to create an application in Xcode that will switch to a new view when the phone is rotated from one orientation to another.

Here is the code for the switchviewcontroller.h file:

#import <UIKit/UIKit.h>

@interface SwitchViewController : UIViewController {

}

-(IBAction)switchview:(id)sender;

@end

And here is the code for the switchviewcontroller.m file:

#import "SwitchViewController.h"
#import "secondview.h"

@implementation SwitchViewController

-(IBAction)switchview:(id)sender {}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if((fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
       (fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight))
    {    
        [[secondview alloc] initWithNibName:@"secondview" bundle:[NSBundle mainBundle]];
    }

}

It works in the iPhone simulator without errors, but when I rotate it does not load the new view. To start, I think I need an application for opening in landscape mode, which I don’t know how to do, but it won’t work anyway, and I think that it has something with the "initWithNibName" part in the code, I have .xib files, not .nib files. Can someone help me with these two things? Thank.

+3
source share
2 answers

, .

secondview *second = [[secondview alloc] initWithNibName:@"secondview" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:second animated:YES];

, .

, :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){

        self.view = landscape;

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
              (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){

        self.view = portrait;

    }

    return YES;
}

, portrait landscape UIViews UIViewController, Interface Builder.


, .h/.m:

.h

IBOutlet UIView *portrait;
IBOutlet UIView *landscape;

@property(nonatomic,retain) UIView portrait;
@property(nonatomic,retain) UIView landscape;

.

@synthesize portrait,landscape;
+5

viewcontroller .

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation) {
        UIView *landscapeView = [[UIView alloc] init];
        // Setup the landscape view here
        self.view = landscapeView;
        [landscapeView release];
    }
    else {
         UIView *portraitView = [[UIView alloc] init];
        // Setup the portrait view here
        self.view = portraitView;
        [portraitView release];
    }
}
+2

Source: https://habr.com/ru/post/1786998/


All Articles