我将尝试在Xcode中创建一个应用程序,当手机从一种方向旋转到另一种方向时,它将切换到新视图。
这是“switchviewcontroller.h”文件的代码:
#import <UIKit/UIKit.h>
@interface SwitchViewController : UIViewController {
}
-(IBAction)switchview:(id)sender;
@end
以下是 "switchviewcontroller.m" 文件的代码:
#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]];
}
}
这个应用在iPhone模拟器上运行没有错误,但是当我旋转它时,它不会加载新的视图。首先,我认为我需要让应用在横向模式下打开,但我不知道如何做,即使这样也不会起作用,我认为问题与代码中的“initWithNibName”部分有关,因为我有.xib文件而不是.nib文件。有人能帮助我解决这两个问题吗?谢谢。