设计具有多个 xib 的视图控制器 [英] Designing a view controller with multiple xibs

查看:23
本文介绍了设计具有多个 xib 的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图控制器中使用多个 xib 进行响应式设计是否是最佳实践?

Is it a best practice to use more than one xib in a view controller for a responsive design?

我目前正在实施一个严重依赖远程 API 获取数据的应用.一个特定的视图令人不安,因为布局取决于从该 API 返回的数据.它要么是具有完整评论和定价的产品,要么是我们仅掌握有限信息的产品.

I'm currently implementing an app that relies heavily on a remote API for its data. One particular view is troubling since the layout depends on the data returned from that API. It is either a product that has a full review and pricing, or a product that we only have limited information on.

设计响应数据.根据返回的信息量,视图的布局与其他视图大不相同.此外,有相当多的元素需要根据文本长度等重新定位.我最初几乎全部在代码中实现了设计,但很快就无法维护,所以我将几乎所有内容都卸载到了 xib 并且只使用了代码重新定位元素.

The design is responsive to the data. Depending on how much information that is returned, the view has a layout that is much different from the other. Also, there's a quite a few elements that need to be repositioned depending on text length, etc. I had originally implemented the design nearly all in code, but that became quickly unmaintainable so I offloaded almost all of it to the xib and only use code to reposition elements.

使用多个 xib 可能是一个不错的解决方案.当视图加载时,控制器执行 API 请求,然后在需要时加载新的 xib.我不确定这种方法可能有什么缺点.

Using more than one xib might be a good solution. When the view loads, the controller performs the API request, then loads the new xib if need be. I'm not sure of what downsides there may be to this approach.

推荐答案

我现在正在实施客户端-服务器应用程序.大多数视图的布局也取决于来自 API 的数据.在像您这样的情况下,我创建了几个 xib 文件(一个用于详细产品,一个用于有限等),并根据服务器响应我选择要加载的 xib.代码示例:

I'm implementing now the client-server app. Layout of the most of views is also depending on data from API. In situation like yours I've created several xib files (one for detailed product, one for limited etc.) and depending on server response I'm choosing xib to load. Code sample:

    // load proper view from nib
    NSArray *nibViews;
    if(product.details) {
        nibViews = [[NSBundle mainBundle] loadNibNamed:@"DetailedView" owner:self options:nil];
    }
    else {
        nibViews = [[NSBundle mainBundle] loadNibNamed:@"NormalView" owner:self options:nil];
    }

    // create and initialize product view object
    ProductView *productView = [nibViews objectAtIndex:0];

    // configure a view here..

您可以将所有版本的视图放入一个 xib 中.在这种情况下,您只需更改 nibViews objectAtIndex 中的索引:

Yo could put all versions of view into one xib. In that case you only change index in nibViews objectAtIndex:

这篇关于设计具有多个 xib 的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆