我正在尝试在 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 文件。有人能帮我解决这两件事吗?谢谢。
你没有推动或呈现任何东西,你只是在初始化视图。
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
and landscape
是 UIViewController 中的 UIView,您在标头中定义并通过 Interface Builder 连接。
此外,这些需要在.h / .m:
.h
IBOutlet UIView *portrait;
IBOutlet UIView *landscape;
@property(nonatomic,retain) UIView portrait;
@property(nonatomic,retain) UIView landscape;
.m
@synthesize portrait,landscape;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)