Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何为多个nib文件使用一个视图控制器_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 如何为多个nib文件使用一个视图控制器

Iphone 如何为多个nib文件使用一个视图控制器,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,所以我整个上午都在想如何在方向改变时从一个视图控制器加载第二个nib文件,但没有成功,这就是为什么我来这里寻求帮助。问题是,当设备旋转时,我想加载一个横向nib文件。到目前为止,我的代码不起作用: -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if (toInterfaceOrientati

所以我整个上午都在想如何在方向改变时从一个视图控制器加载第二个nib文件,但没有成功,这就是为什么我来这里寻求帮助。问题是,当设备旋转时,我想加载一个横向nib文件。到目前为止,我的代码不起作用:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight || toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        [self initWithNibName:@"myNibFile" bundle:nil];
    }
}
未加载新的nib。在interface builder中,我将文件所有者设置为该视图控制器,因此它应该可以工作。我还尝试了:
[[NSBundle mainBundle]loadNibNamed:@“myNibFile”所有者:self选项:nil];
但我也不走运。您能提供一个示例,说明如何从initWithNibName或有用的链接加载nib文件吗?我似乎没有在网上找到它


非常感谢。

为此,您已经创建了类的新实例。您不能在运行时更改视图控制器的nib


为什么要为一个视图控制器创建多个nib文件。只需在单个nib文件和用户中创建多个视图。

为此,您已经创建了类的新实例。您不能在运行时更改视图控制器的nib


为什么要为一个视图控制器创建多个nib文件。只需在单个nib文件中创建多个视图,并相应地使用户。

在运行时更改控制器的nib根本不是一个好主意(不确定是否合法)。它可能导致笔尖在内存中悬空&最终导致内存泄漏。不久前我遇到了同样的问题&发现不使用IB&nibs是最简单的方法(幸运的是我的观点并不复杂)

解决方案是在代码中布局子组件,并根据方向变化进行调整。我在UIViewController子类中执行了以下操作-

-(void) adjustLayout:(UIInterfaceOrientation)orientation
{
    if(UIInterfaceOrientationIsPortrait(orientation))
    {
         mySubview1.frame = frameInAccordanceWithPortrait;
    }
    else
    {
        mySubview1.frame = frameInAccordanceWithPortrait;
     }
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self adjustLayout:toInterfaceOrientation];
} 

在运行时更改控制器的nib根本不是一个好主意(不确定是否合法)。它可能导致笔尖在内存中悬空&最终导致内存泄漏。不久前我遇到了同样的问题&发现不使用IB&nibs是最简单的方法(幸运的是我的观点并不复杂)

解决方案是在代码中布局子组件,并根据方向变化进行调整。我在UIViewController子类中执行了以下操作-

-(void) adjustLayout:(UIInterfaceOrientation)orientation
{
    if(UIInterfaceOrientationIsPortrait(orientation))
    {
         mySubview1.frame = frameInAccordanceWithPortrait;
    }
    else
    {
        mySubview1.frame = frameInAccordanceWithPortrait;
     }
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self adjustLayout:toInterfaceOrientation];
} 

即使您做到了这一点,您的插座都将重新连接到新nib中的视图组件,因此,一旦加载新nib,您在现有视图中设置的任何值(例如文本字段、按钮状态)都将丢失。我建议您了解interface builder中大小检查器的大小、定位和自动调整大小功能-通过正确设置这些功能,可以让视图在旋转时自动调整其布局,这样可以实现很多功能。即使您做到了这一点,您的所有插座都将重新连接到新nib中的视图组件,因此一旦加载新nib,您在现有视图中设置的任何值(例如文本字段、按钮状态)都将丢失。我建议您了解interface builder中大小检查器的大小、定位和自动调整大小功能-通过正确设置这些功能,可以让视图在旋转时自动调整其布局,从而取得很大的效果。