尝试在方向更改时加载新视图 [英] Trying to load new view upon orientation change

查看:89
本文介绍了尝试在方向更改时加载新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Xcode中创建一个应用程序,当手机从一个方向旋转到另一个方向时,它将切换到一个新视图。

I am trying to create an application in Xcode that will switch to a new view when the phone is rotated from one orientation to another.

这是switchviewcontroller .h文件代码:

Here is the "switchviewcontroller.h" file code:

#import <UIKit/UIKit.h>

@interface SwitchViewController : UIViewController {

}

-(IBAction)switchview:(id)sender;

@end

这里是switchviewcontroller.m文件代码:

And here is the "switchviewcontroller.m" file code:

#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文件。有谁可以帮我这两件事?谢谢。

It runs in iPhone simulator with no errors, but, when I rotate it doesn't load the new view. For starters I think I need the app to open in landscape mode, which I don't know how to do, but it still wouldn't work anyway and I think it has something to to with the "initWithNibName" part of the code because I have .xib files and not .nib files. Can anyone help me with these 2 things? Thanks.

推荐答案

你不是在推动或展示任何东西,你只是在开始观看。

You are not pushing or presenting anything, you are only init'ing the view.

secondview *second = [[secondview alloc] initWithNibName:@"secondview" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:second animated:YES];

但是,它不是展示新视图的好地方。

However, its not a good place to show a new view.

如果您想以不同的方向显示相同的视图,请尝试以下操作:

If you want to show the same view in a different orientation, try something this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){

        self.view = landscape;

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
              (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){

        self.view = portrait;

    }

    return YES;
}

请注意 portrait landscape 是您在UIViewController中的UIViews,您可以在标题中定义并通过Interface Builder进行连接。

Note that portrait and landscape are UIViews in your UIViewController that you define in your header and connect through Interface Builder.

另外,这些需要在 .h / .m

Additionaly, these need to be in .h / .m:

IBOutlet UIView *portrait;
IBOutlet UIView *landscape;

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



.m



.m

@synthesize portrait,landscape;

这篇关于尝试在方向更改时加载新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆