【问题标题】:Two views for a single view controller单个视图控制器的两个视图
【发布时间】:2013-03-11 05:12:43
【问题描述】:

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

【问题讨论】:

    标签: iphone ios objective-c uiview uiviewcontroller


    【解决方案1】:

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

    第一

    在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”。

    只要设备旋转,该方法就会调用。

    希望对你有所帮助。

    谢谢。

    【讨论】:

    • 感谢您的回复。但我正在考虑使用两种不同的视图,而不是调整对象的大小/重新定位。否则我在每个屏幕上都进行这种更改会非常乏味,因为它是一个丰富的 UI。
    • 但是,正如您之前所说,您必须使用相同的对象(即按钮),那么您将如何管理同一对象的多个连接。然后你需要调整控件的大小。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 感谢您的回复。我仍然有同样的问题。它正在加载一个空的纵向视图
    • 你给了 iboulets 视图,你什么时候添加子视图,我的意思是用哪种方法
    • 另外你需要在 self.view 中添加两个视图。
    • 我已经修改了问题,并添加了指向问题示例解决方案的链接。你能看看吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    相关资源
    最近更新 更多