【问题标题】:Want to use muliple nibs for different iphone interface orientations想要为不同的 iphone 界面方向使用多个笔尖
【发布时间】:2011-04-16 17:58:36
【问题描述】:

我有一个情况,我创建了两个不同的笔尖,一个在纵向模式下,另一个在横向模式下。我在视图中有很多设计,所以我不得不选择两种不同的笔尖。现在,我想在界面在

中旋转时切换笔尖

通用视图控制器

这样我就可以保留视图中控件的填充值和状态。

我现在正在使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
    [self initWithNibName:@"LandscapeNib" bundle:nil];
}else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
    [self initWithNibName:@"PortraitNib" bundle:nil];

}


return YES;

}

但它不会改变笔尖,它会显示初始加载的笔尖。我猜它正在加载但没有显示,因为初始笔尖已经显示,没有被删除。我找不到使用通用视图控制器处理多个笔尖的解决方案,以便我可以轻松处理控件的功能?

【问题讨论】:

    标签: iphone ipad orientation


    【解决方案1】:

    我需要为我自己的应用程序做同样的事情,最后得到一个特定的 UIAutoRotateView 类来为你处理。

    见解释here

    希望这会有所帮助。

    【讨论】:

    • 只包含链接的答案是considered bad practice。请在此处总结内容(请勿复制/粘贴),以便答案可以独立存在。如果您不这样做,您的答案将面临被删除的风险,尤其是在链接失效的情况下。另外,请查看faq 链接到您自己的博客。
    【解决方案2】:

    您应该为横向模式创建一个特殊的视图控制器,并在设备旋转时以模态方式呈现它。要在设备旋转时收到通知,请注册UIDeviceOrientationDidChangeNotification 通知。

    对于需要在纵向和横向中以不同方式呈现的复杂视图,这是 Apple 推荐的实现方式。阅读此处了解更多详情:

    http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW26

    这是来自 Apple 文档的 sn-p。在您的 init 或 awakeFromNib 中注册通知:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
       [[NSNotificationCenter defaultCenter] addObserver:self
                                 selector:@selector(orientationChanged:)
                                 name:UIDeviceOrientationDidChangeNotification
                                 object:nil];
    

    并在orientationChanged中以模态方式呈现您的横向视图控制器,或者将其关闭,所有这些都根据当前的旋转:

    - (void)orientationChanged:(NSNotification *)notification
    {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
            !isShowingLandscapeView)
        {
            [self presentModalViewController:self.landscapeViewController
                                    animated:YES];
            isShowingLandscapeView = YES;
        }
        else if (deviceOrientation == UIDeviceOrientationPortrait &&
                 isShowingLandscapeView)
        {
            [self dismissModalViewControllerAnimated:YES];
            isShowingLandscapeView = NO;
        }
    }
    

    【讨论】:

      【解决方案3】:

      shouldAutorotateToInterfaceOrientation: 应该简单地返回 YES 或 NO。根据我的经验,在这里进行任何扩展处理都不是一个好主意。来自文档:

      您对此方法的实现 应该简单地返回基于 YES 或 NO 关于中的价值 接口方向参数。不要 尝试获取 interfaceOrientation 属性或检查 由报告的方向值 UIDevice 类。你的视图控制器 要么能够支持 给定方向,否则不是。

      为响应设备旋转加载笔尖的更好位置是

      willRotateToInterfaceOrientation:duration:didRotateToInterfaceOrientation:

      【讨论】:

        【解决方案4】:

        一旦 self 已经被初始化,你就不能再次初始化 self。

        旋转时,您需要:

        1. 使用loadNibNamed:owner:options: 用手将笔尖装入 NSArray。 (见the documention)。
        2. 将 self.view 设置为索引处的对象 该数组的 0 个。

        【讨论】:

        • 即使这可能有效,我也不建议更换控制器的视图。这听起来很脏,引入了脆弱的情况,在正确释放和保留对象时很容易出错。
        • 如果希望用户再次旋转设备,实际上替换控制器视图会好得多。它不使用那么多内存,而且旋转会更快。此外,如果两个视图都是类对象,它们可以通过视图控制器释放和释放。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多