以编程方式更改UIViewController视图NIB的最佳方法 [英] Best way to change UIViewController view NIB programmatically

查看:137
本文介绍了以编程方式更改UIViewController视图NIB的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPad应用程序包含不同的NIB和视图。
每个视图都有两个NIB,一个用于potrait方向,一个用于landscape,所以我正在寻找一种方法来以编程方式为给定的UIViewController切换NIB。

I have this iPad application with different NIBs and views. Each view has two NIBs, one for potrait orientation and one for landscape, so i'm searching for a method to programmatically switch NIB for a given UIViewController .

现在我有这个函数,我在每个控制器的willRotateToInterfaceOrientation方法中使用:

Right now i have this function that i'm using in the willRotateToInterfaceOrientation method of each controller :

void UIHandleRotation( UIViewController *controller, NSString *nibName, UIInterfaceOrientation orientation, BOOL transform ){
    double angle = 0.0;

    if( orientation == UIInterfaceOrientationPortrait ) {
        angle = PI * 2 /* 360° */;
    } 
    else if( orientation == UIInterfaceOrientationLandscapeLeft ){
        angle   = PI + PI/2 /* 270 ° */;
        // Each landscape nib is [nib-name]L
        nibName = [NSString stringWithFormat:@"%@L", nibName];
    }    

    [[NSBundle mainBundle] loadNibNamed:nibName owner:controller options:nil];
    if( transform ) {
        controller.view.transform = CGAffineTransformMakeRotation( angle );
    } 
}

但它给了我奇怪的行为(ui控制位置搞砸了,出口没有在界面构建器中关联,等等。

But it's giving me strange behaviour (ui controls position messed, the outlets are not associated as in interface builder, and so forth).

我做错了什么或者只是有更好的方法来实现它?
谢谢

Am i doing something wrong or there's just a better way to implement this ? Thanks

注意:我没有使用导航控制器,因此我无法使用此解决方案最简单的方法来支持多个方向?如何在应用程序处于横向时加载自定义NIB?

NOTE: I'm not using a navigation controller, so i can't use this solution Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?

推荐答案

您不应该切换笔尖,你应该切换控制器。

You shouldn't switch nibs, you should switch controllers.

请参阅以下答案:支持多种方向的最简单方法

这篇关于以编程方式更改UIViewController视图NIB的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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