• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 方向改变时交换 UIViewControllers 的问题

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:53:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有 2 个 View Controller 的单 View 应用程序,用于呈现纵向和横向的不同布局。我已经设置了方向更改通知,并且可以在第一次方向更改时成功显示横向 View 。

第一个问题: 当我将方向更改回纵向时,不显示纵向 View 。

第二个问题: 当我将方向更改回横向时,横向 View 会显示,但我收到警告:

尝试在其 View 不在窗口层次结构中的 CalculatorViewController 上呈现 CalculatorViewControllerLandscape。

我浏览了苹果文档和几篇有类似问题的帖子,发现答案在于使用委托(delegate),但我无法正确设置委托(delegate)。这是我的尝试:

CalculatorViewControllerLandscape.h

@protocol SecondControllerDelegate <NSObject>

@end
.....
@property(nonatomic, weak) id <SecondControllerDelegate> delegate;

CalculatorViewController.h

@interface CalculatorViewController : UIViewController <SecondControllerDelegate> {
....
}
@property (strong) CalculatorViewControllerLandscape *landscapeVC;

CalculatorViewCalculator.m

- (void)awakeFromNib
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(orientationChanged
                                             name:UIDeviceOrientationDidChangeNotification     
                                           object:nil];
// register as a delegate
self.navigationController.delegate = (id)self;
}

- (void)orientationChangedNSNotification *)notification
{
   UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
           !isShowingLandscapeView)        
    {
        NSLog(@"Orientation has changed to landscape");
        // code here to show landscape storyboard
        UIStoryboard *landscapeStoryboard = [UIStoryboard storyboardWithName"LandscapeStoryboard" bundle:nil];
        UIViewController *landscapeViewController = [landscapeStoryboard instantiateInitialViewController];
         [self presentViewController:landscapeViewController animated:YES completion:nil];
         isShowingLandscapeView = YES;
    }    
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&             
         isShowingLandscapeView)        
    {
        NSLog(@"Orientation has changed to portrait");   
    [[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
    isShowingLandscapeView = NO;
    }
}

我已经为此工作了几个小时,并检查了所有类似问题的帖子,但我仍然无法弄清楚。提前感谢您的帮助。



Best Answer-推荐答案


最佳实践是在单个 UIViewController 中处理旋转事件,而不是使用两个单独的事件。我不熟悉界面生成器,但您可以通过编程方式覆盖 -(void)viewWillLayoutSubviews; 并根据 self.interfaceOrientation 适本地布置您的 View 。我建议你这样做。

但是,在回答您的问题时:

尝试改变

[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];

[self dismissViewControllerAnimated:NO completion:nil];

这也可以解决第二个问题,因为旧的横向 View Controller 没有被正确关闭。

关于ios - 方向改变时交换 UIViewControllers 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18501423/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap