加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 尝试在方向更改时加载新视图

发布时间:2020-12-16 09:31:52 所属栏目:百科 来源:网络整理
导读:我正在尝试在 Xcode中创建一个应用程序,当手机从一个方向旋转到另一个方向时,该应用程序将切换到新视图. 这是“switchviewcontroller.h”文件代码: #import UIKit/UIKit.h@interface SwitchViewController : UIViewController {}-(IBAction)switchview:(id)
我正在尝试在 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;
}

请注意,纵向和横向是UIViewController中的UIViews,您可以在标头中定义并通过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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读