Objective c 如何使用一个ViewController文件加载横向xib?
我有一个通用的应用程序。我对纵向和横向视图使用单独的XIB。当我在风景区时,我让应用程序检测方向并将Objective c 如何使用一个ViewController文件加载横向xib?,objective-c,ios,ipad,xib,ios-universal-app,Objective C,Ios,Ipad,Xib,Ios Universal App,我有一个通用的应用程序。我对纵向和横向视图使用单独的XIB。当我在风景区时,我让应用程序检测方向并将BOOL的值更改为true。我想知道当BOOL为真时如何加载我的横向xib。我尝试了几种不同的方法来实现这一点,但没有任何效果。对此问题的任何意见都将不胜感激。我可以更新这篇文章以包含任何必要的代码片段。提前谢谢 编辑:我想在一个ViewController类中完成所有这些,并且只针对iPad。。。不是iPhone。那部分我都搞定了。我只需要加载横向xib 编辑:在我的viewDidLoad中,我
BOOL
的值更改为true。我想知道当BOOL
为真时如何加载我的横向xib。我尝试了几种不同的方法来实现这一点,但没有任何效果。对此问题的任何意见都将不胜感激。我可以更新这篇文章以包含任何必要的代码片段。提前谢谢
编辑:我想在一个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
编辑
看起来你需要在这里工作
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];
编辑
看起来你需要在这里工作
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。我已经将名为“景观”的布尔值设置为“是”。我已经尝试在ViewWillDisplay中加载xib,我甚至尝试在viewDidLoad中设置一个if语句,试图让它覆盖原始的xib。我陷入了一个无限循环。是否要以某个方向加载viewController?或者你正在尝试处理方向变化?当方向为横向时,我希望它加载横向xib而不是纵向xib。方向的变化我已经控制住了。当应用程序加载时,我可以从纵向xib更改为横向xib,而不会出现问题。当我在风景画中时,我只想完全绕过肖像画xib。如果我能解决这个问题。。。从那时起,我可以通过风景线,成为肉汁。那么你试过上面的代码了吗?两者都将加载正确的笔尖。或者将第一个方法放在viewcontrollerinit方法中使用,或者在实际alloc/init视图控制器的位置使用第二个方法我尝试了它,但我没有init方法。我尝试在viewDidLoad中执行一些操作,但这会导致我陷入无限循环。我尝试创建一个init方法,但它从未被调用,除非我在viewDidLoad中调用它。。。这也会导致循环。我以前从未尝试过这样做。。。这就是为什么我有这么多麻烦。哈哈,我被新的锡伯胶装在哪里卡住了。我已经将名为“景观”的布尔值设置为“是”。我已经尝试在ViewWillDisplay中加载xib,我甚至尝试在viewDidLoad中设置一个if语句,试图让它覆盖原始的xib。我陷入了一个无限循环。是否要以某个方向加载viewController?或者你正在尝试处理方向变化?当方向为横向时,我希望它加载横向xib而不是纵向xib。方向的变化我已经控制住了。当应用程序加载时,我可以从纵向xib更改为横向xib,而不会出现问题。当我在风景画中时,我只想完全绕过肖像画xib。如果我能解决这个问题。。。从那时起,我可以通过风景线,成为肉汁。那么你试过上面的代码了吗?两者都将加载正确的笔尖。或者将第一个方法放在viewcontrollerinit方法中使用,或者在实际alloc/init视图控制器的位置使用第二个方法我尝试了它,但我没有init方法。我尝试在viewDidLoad中执行一些操作,但这会导致我陷入无限循环。我尝试创建一个init方法,但它从未被调用,除非我在viewDidLoad中调用它。。。这也会导致循环。我以前从未尝试过这样做。。。这就是为什么我有这么多麻烦。哈哈。