在方向改变时尝试加载新视图

3

我将尝试在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文件。有人能帮助我解决这两个问题吗?谢谢。


欢迎来到 StackOverflow。当粘贴代码时,请使用“{ }”按钮对其进行正确缩进,以便正确格式化它。 - Dave DeLong
2个回答

5

你并未推送或呈现任何内容,你只是在初始化视图。

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;
}

请注意,portraitlandscape是您在头文件中定义并通过Interface Builder连接的UIViewController中的UIView。
另外,它们需要在.h / .m文件中:
IBOutlet UIView *portrait;
IBOutlet UIView *landscape;

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

.m

@synthesize portrait,landscape;

谢谢你的帮助,我不想在这里听起来很蠢,但我把你的代码放进去后,应用程序的行为完全相同,我知道你是对的,但我就是想不出我做错了什么。 - Baccalad
首先,当设备旋转时,我不会推送一个视图,可以选择第二个示例。如我所述,您需要在标头中定义视图,并完成整个过程。我将使用标头代码更新我的答案。 - WrightsCS
这会加载一个新视图还是在新方向上加载相同的视图。我问这个问题是因为我想能够例如(尽管我并没有特别这样做)在纵向显示一张狗的图片,在横向显示高分表。 - Baccalad
它将以新的方向显示一个新的视图。纵向/横向视图可以相同,只需重新排列即可。 - WrightsCS
我将代码实现到一个新的应用程序中,但是该应用在打开时崩溃了。 - Baccalad

2

你需要分配并初始化一个新的视图,并用新的视图替换当前视图控制器的视图。

- (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];
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接