【问题标题】:Two UIViews in one .xib file?一个 .xib 文件中有两个 UIView?
【发布时间】:2010-04-23 18:50:50
【问题描述】:

我在 mei .xib 文件中创建了第二个 uiview。第一个视图是风景,我通过以下代码得到它

ItemController *newItem;
newItem = [[ItemController alloc] init];
newItem.view.....

我怎样才能“激活”第二个视图,这样我就可以使用它了

newItem.view2...

这可能吗?第二个视图是肖像模式,所以它应该被隐藏,当转动 ipad 时,第一个视图应该被隐藏,第二个可见。

谢谢

【问题讨论】:

    标签: objective-c uiview ipad interface-builder xib


    【解决方案1】:

    如果我正确理解你的问题,你可以使用 UIViewController 中的willRotateToInterfaceOrientation:duration: 方法。

    您可以声明两个 UIView 变量并使用 IB 设置它们的连接:

    IBOutlet UIView *view1;
    IBOutlet UIView *view2;
    

    然后,在willRotateToInterfaceOrientation:duration: 中,您可以交换视图。假设你的 ItemController 是 UIViewController 的一个子类,你可以有这样的东西:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            [self setView:view1];
        }
        else {
            [self setView:view2];
        }
    }
    

    当用户旋转 iPad 时,该方法将被自动调用。

    注意:确保将shouldAutorotateToInterfaceOrientation: 设置为返回YES,并且您可能必须检查方向以将视图属性设置为最初的正确视图。

    编辑:我的回答是假设您有两个具有不同布局/元素的视图,而不是两个视图对于不同方向的大小基本相同。如果是后者,则可能有更好的方法来仅使用一个视图。

    【讨论】:

      【解决方案2】:

      当您有一些IBActions 与单击按钮或更改标签文本(在 xib 上)等相关联时,您所采取的方法将导致您陷入困境。 最好的方法是在方向改变时改变框架高度宽度和位置。虽然一开始比较困难,但是可以省去很多麻烦。

      如需帮助,请查看Rotate UIViewController to counteract changes in UIInterfaceOrientation

      希望这会有所帮助。

      谢谢,

      马杜普

      【讨论】:

        猜你喜欢
        • 2015-06-04
        • 1970-01-01
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 1970-01-01
        相关资源
        最近更新 更多