想要将muliple笔尖用于不同的iphone接口方向

Ved*_*ash 5 iphone orientation ipad

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

常见的viewController

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

现在我正在使用

- (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;
Run Code Online (Sandbox Code Playgroud)

}

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

Mic*_*hal 6

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

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

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

这是Apple文档的一个片段.在init或awakeFromNib中注册通知:

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

并且在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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Tom*_*omH 5

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

您对此方法的实现应该只根据interfaceOrientation参数中的值返回YES或NO.不要尝试获取interfaceOrientation属性的值或检查UIDevice类报告的方向值.您的视图控制器要么能够支持给定的方向,要么不支持.

一个更好的地方加载你的笔尖响应设备轮换将是

willRotateToInterfaceOrientation:duration: 要么 didRotateToInterfaceOrientation:


Alc*_*ive 2

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

旋转时,您需要:

  1. 用于loadNibNamed:owner:options:手动将笔尖加载到 NSArray 中。(参见文档)。
  2. 将 self.view 设置为该数组索引 0 处的对象。