单个视图控制器的两个视图

时间:2013-03-11 05:12:44

标签: iphone ios objective-c uiview uiviewcontroller

我对iOS编程完全陌生。我正在开发一个应用程序,我需要一个不同的肖像视图和另一个横向视图。即使视图不同,两个视图中的按钮和其他控件也是相同的。所以我想保持两个视图中从按钮/标签到视图控制器的相同连接。因此,如果我在视图控制器中更改标签的值,它应该在视图中更改,而不管我呈现的视图。

现在我遇到的问题是我无法为故事板中的项目添加两个不同的根视图。我试图在主视图中添加两个不同的子视图。这样我就可以创建视图,但是我无法将两个视图中的按钮连接到单个连接。例如,

@property (strong, nonatomic) IBOutlet UIButton *buttonNext;    

只能连接到一个视图中的下一个按钮。

为了解决这个问题,我在故事板中创建了两个不同的视图,为两者添加了相同的视图控制器。然后为每个视图创建一个segue,如下所示:

@property (strong, nonatomic) IBOutlet UIView *portraitView;
@property (strong, nonatomic) IBOutlet UIView *landscapeView;

在我的视图控制器中,我添加了:

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

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
//NSLog(self.view.restorationIdentifier);
if (orientation == UIInterfaceOrientationMaskLandscape || orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
    if(self.landscapeView == nil)
    {
        self.landscapeView =[[UIView alloc] init];
    }
   self.view = landscapeView;
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    self.view = portraitView;
}
}

问题是第二个视图未正确加载。最初它被加载为零。即使我在代码中初始化视图,也不会初始化视图中的子视图/控件。过去三天我一直坚持这个。任何帮助将不胜感激。

编辑1:

我在这里添加了示例代码:http://cl.ly/2z3f3E290f0R

2 个答案:

答案 0 :(得分:3)

你可以用单一视图来试试这个:

<强>第一

viewDidLoad方法中,设备方向

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

<强>第二

OrientationChanged

的方法
-(void)orientationChanged {

    //Check for Portrait Mode
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
       x=25; y=30;
    }
    //Lanscape mode
    else {
       x=50; y=40;
    }

    [button setFrame:CGRectMake(x, y, 80, 80)];
}

在这里,你可以采取&#39; X&#39; &安培; &#39; Y&#39;无论你想要什么。

此方法始终在设备旋转时调用。

希望它会对你有帮助。

感谢。

答案 1 :(得分:0)

试试这个

 UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
        if (newOrientation != UIDeviceOrientationUnknown && newOrientation != UIDeviceOrientationFaceUp && newOrientation != UIDeviceOrientationFaceDown)
        {
            if(orientation != newOrientation)
            {
                //ORIENTATION HAS CHANGED. Do orientation logic
                if(
                   (newOrientation == UIDeviceOrientationLandscapeLeft || newOrientation == UIDeviceOrientationLandscapeRight)&&(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown))
                {
                    NSLog(@"Changed orientation to Landscape");
                    // Clear the current view and insert the orientation-specific view
                    [self clearCurrentView];
                    [self.view insertSubview:titleLandscapeView atIndex:0];
                }
                else if (
                         (newOrientation == UIDeviceOrientationPortrait || newOrientation == UIDeviceOrientationPortraitUpsideDown)&&(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight))
                {
                    NSLog(@"Changed orientation to Portrait");
                    // Clear the current view and insert the orientation-specific view
                    [self clearCurrentView];
                    [self.view insertSubview:titlePortraitView atIndex:0];
                }
                orientation = newOrientation;
            }

 -(void) clearCurrentView{
        if (titleLandscapeView.superview){
            NSLog(@"clear titleLandscapeView");
            [titleLandscapeView removeFromSuperview];
        } else if (titlePortraitView.superview){
            NSLog(@"clear titlePortraitView");
            [titlePortraitView removeFromSuperview];
        } else
        {
            NSLog(@"Neither titleLandscapeView nor titlePortraitView");
        }
    }