如何使用一个 ViewController 文件加载横向 xib?
Posted
技术标签:
【中文标题】如何使用一个 ViewController 文件加载横向 xib?【英文标题】:How can I load a landscape xib using one ViewController file? 【发布时间】:2012-05-03 16:02:47 【问题描述】:我有一个通用应用程序。我为纵向和横向视图使用单独的 xib。当我处于横向时,我有应用程序检测方向并将 BOOL
的值更改为 true。我想知道当BOOL
为真时如何加载我的景观xib。我尝试了几种不同的方法来实现这一点,但没有任何效果。对此问题的任何意见将不胜感激。我可以更新这篇文章以包含任何必要的代码 sn-ps。提前致谢。
编辑:我想在一个 ViewController 类中完成所有这些工作,并且仅适用于 iPad……而不是 iPhone。我已经解决了所有问题。我只需要加载横向 xib。
编辑:在我的viewDidLoad
我正在这样做:
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight)
landscape = YES;
这是我的主视图控制器 .m:
@implementation PassportAmericaViewController
@synthesize browseViewButton, webView, mainView, lblMemberName, menuOpen, internetActive, hostActive, isUsingiPad, portrait, landscape;
- (void)viewDidLoad
menuOpen = NO;
UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
isUsingiPad = YES;
if (isUsingiPad)
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight)
landscape = YES;
[self checkForKey];
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
-(void)viewWillAppear:(BOOL)animated
[self.navigationController setNavigationBarHidden:YES];
-(void) checkForKey
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int regCheck = [defaults integerForKey:@"registration"];
if (isUsingiPad)
if (regCheck == 0)
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:@"RegistrationView-iPad" bundle:[NSBundle mainBundle]];
regView.isUsingiPad = YES;
[self.navigationController pushViewController:regView animated:YES];
else if (regCheck == 1)
@try
NSString *mbrFirstName = [defaults objectForKey:@"firstName"];
NSString *mbrLastName = [defaults objectForKey:@"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:@" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
@catch (NSException *exception)
else
if (regCheck == 0)
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:@"RegistrationView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:regView animated:YES];
else if (regCheck == 1)
@try
NSString *mbrFirstName = [defaults objectForKey:@"firstName"];
NSString *mbrLastName = [defaults objectForKey:@"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:@" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
@catch (NSException *exception)
-(IBAction) openBrowseView
if (isUsingiPad && landscape)
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:@"BrowseView-iPadLandscape" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
else if (isUsingiPad)
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:@"BrowseView-iPad" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
else
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:@"BrowseView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:browseView animated:YES];
-(IBAction) openViewMore
if (isUsingiPad)
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:@"ViewMoreView-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
viewMoreView.isUsingiPad = YES;
else
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:@"ViewMoreView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
-(IBAction) callTollFree:(id)sender
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:8002837183"]];
-(IBAction)clickToJoin:(id)sender
if (isUsingiPad)
webView = [[WebViewController alloc]
initWithNibName:@"WebView-iPad" bundle:[NSBundle mainBundle]];
webView.url=@"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
webView.isUsingiPad = YES;
else
webView = [[WebViewController alloc]
initWithNibName:@"WebView" bundle:[NSBundle mainBundle]];
webView.url=@"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
-(IBAction) iPadContactUs:(id)sender
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"mailto:info@passport-america.com"]];
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
if ([animationID isEqualToString:@"slideMenu"])
UIView *sq = (__bridge UIView *) context;
[sq removeFromSuperview];
-(void) positionViews
if (isUsingiPad)
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown)
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:@"PassportAmericaViewController-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = YES;
homeView2.landscape = NO;
else
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:@"PassportAmericaViewController-iPadLandscape" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = NO;
homeView2.landscape = YES;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if (isUsingiPad)
return YES;
else
// Return YES for supported orientations
return NO;
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration
if (isUsingiPad)
[self positionViews];
else
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
@end
【问题讨论】:
【参考方案1】:编辑
看来你需要在这里做你的工作
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? @"landscapeNibName" : @"portraitNibName";
RegistrationViewController *regView = [[RegistrationViewController alloc] initWithNibName:nibName bundle:[NSBundle mainBundle]];
我不确定你在哪里卡住了......
- (id)init;
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? @"landscapeNibName" : @"portraitNibName";
self = [super initWithNibName:nibName bundle:nil];
if (self)
// any other init stuff
return self;
或者如果您更喜欢在实例化时命名 nib
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? @"landscapeNibName" : @"portraitNibName";
MyViewController *viewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];
【讨论】:
我不知道在哪里加载新的 xib。我已经将名为 Landscape 的 BOOL 设置为 yes。我尝试在 viewWillAppear 中加载 xib,我什至尝试在 viewDidLoad 中设置 if 语句以尝试让它覆盖原始 xib。我陷入了无限循环。 你想在某个方向加载 viewController 吗?还是您正在努力应对方向变化? 当方向为横向时,我希望它加载横向 xib 而不是纵向 xib。我可以控制方向的变化。当应用程序加载时,我可以毫无问题地从纵向 xib 更改为横向 xib。当我处于横向时,我只想完全绕过纵向 xib。如果我解决了...我可以通过该景观 BOOL 并从那时起成为肉汁。 那么上面的代码你试过了吗?两者都将加载正确的笔尖。要么通过将它放在 viewcontrollers 的 init 方法中使用第一种方法,要么在你实际分配/初始化视图控制器的地方使用第二种方法 我试过了,但是我没有init方法。我尝试在 viewDidLoad 中做一些事情,但这会导致我陷入无限循环。我尝试创建一个 init 方法,但它永远不会被调用,除非我在 viewDidLoad 中调用它......这也会导致循环。我以前从未尝试过这样做……这就是我遇到这么多麻烦的原因。哈哈。以上是关于如何使用一个 ViewController 文件加载横向 xib?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 segue 从 Xib 文件加载 viewController?斯威夫特 5
自定义viewcontroller怎么用storyboard加导航
如何在使用它的 ViewController 上调整自定义(在单独的文件中创建)UIView 高度?
如何将值从 SceneDelegate 文件传输到 ViewController (Swift 5)