0

我对 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

4

2 回答 2

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)];
}

在这里,您可以随心所欲地使用“X”和“Y”。

每当设备旋转时,此方法总是调用。

希望对您有所帮助。

谢谢。

于 2013-03-11T05:21:30.390 回答
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");
        }
    }
于 2013-03-11T07:27:32.060 回答