简体   繁体   English

如何在同一视图控制器(iOS)中将具有相同元素的不同UI应用于?

[英]How to apply different UI with same elements in same view controller (iOS)?

I'm using Swift in Xcode 7 with Storyboard. 我在带有Storyboard的Xcode 7中使用Swift。 I have different UI with same elements in same view controller. 我在同一视图控制器中具有相同元素的不同UI。 Depending on the data fetched from server, I wish to display different UI layout. 根据从服务器获取的数据,我希望显示不同的UI布局。

To make it easier to understand, eg I have one view controller with one label aligned the bottom of the view. 为了更容易理解,例如,我有一个视图控制器,其中一个标签与视图底部对齐。 While data is fetched from server, I want to display the label with different layout: 从服务器获取数据时,我想以不同的布局显示标签:

  • If server returns layout as bottom , I'll apply the layout of text as align bottom and centre horizontally 如果服务器将layout返回为bottom ,则将文本的布局应用为align bottomcentre horizontally align bottom
  • If server returns layout as centre , I want to apply the layout of text as centre vertically and centre horizontally 如果服务器返回layoutcentre ,我想将文本的布局centre vertically centre horizontallycentre horizontally

As a result, if I have many elements, it becomes hard for me to code the NSLayoutConstraint manually. 结果,如果我有很多元素,则很难手动编写NSLayoutConstraint。 Is there anyway I can still use Interface Builder to have different UI which applied to same view controller? 无论如何,我仍然可以使用Interface Builder来将不同的UI应用于同一视图控制器吗?

I've had the same issue before and managed to come with a solution, though it is probably not how it was intended by apple. 我以前也遇到过同样的问题,并设法提出了一个解决方案,尽管它可能不是苹果打算的。 I tested and it works with xcode 6 and 7, possibly earlier versions too. 我进行了测试,它可以与xcode 6和7一起使用,可能也可以是早期版本。 Your question does not say if you are working with nibs or storyboards, but the answer I'm providing is for nibs. 您的问题并未说明您是使用笔尖还是情节提要,但我提供的答案是针对笔尖的。 To set up your nib you need to add 2 view controllers or more.Set up your view controllers the way you need them to look and set the view controllers' main view's tag so you can differentiate them. 要设置笔尖,您需要添加2个或更多的视图控制器。以所需的方式设置视图控制器的外观并设置视图控制器的主视图标签,以便区分它们。 To be able to connect your IBOutlets you will need to apply a bit of a trick: 为了能够连接您的IBOutlets,您将需要一些技巧:

  • You set the first view controller's class so you can connect the outlets. 您可以设置第一个视图控制器的类,以便可以连接插座。
  • You connect the outlets. 您连接插座。 You delete the class name in the attribute inspector. 您在属性检查器中删除类名称。 (Note that the outlets in the nib stay connected, but in the .m or .h file they show they are not. If you try to build your project now it will fail.) (请注意,笔尖中的插座保持连接状态,但是在.m或.h文件中,它们显示没有连接。如果您现在尝试构建项目,它将失败。)
  • You set the second view controller's class so you can connect the outlets again. 设置第二个视图控制器的类,以便可以再次连接插座。
  • You connect the outlets again. 您再次连接插座。 You re-enter the class name in the attribute inspector for the first view controller. 您在属性检查器中为第一个视图控制器重新输入类名称。

Now you just need to get to your view controllers. 现在,您只需要进入视图控制器即可。 For this I use a class method to get an instance of the view controller: 为此,我使用类方法来获取视图控制器的实例:

+ (UIViewController *)customViewControllerForLayout:(NSInteger)layoutTag
{
    NSArray *customViewControllers = [[NSBundle mainBundle] loadNibNamed:@"Your nib's name" owner:self options:nil];

    __block UIViewController *customViewControllerWithSpecifiedLayout = nil;
    [customViewControllers enumerateObjectsUsingBlock:^(UIViewController *customViewController, NSUInteger index, BOOL *stop) {
        if (customViewController.view.tag == layoutTag) {
            customViewControllerWithSpecifiedLayout = customViewController;
            *stop = YES;
        }
    }];

    return customViewControllerWithSpecifiedLayout;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM