【问题标题】:Loading different views for iPad & iPhone in Titanium Alloy在钛合金中为 iPad 和 iPhone 加载不同的视图
【发布时间】:2014-05-19 10:43:27
【问题描述】:

我为 iPad 和 iPhone 创建了不同的视图。文件夹层次结构是views/ios/iPhone/index.xml 和views/ios/iPad/index.xml

我只有一个控制器 index.js。

我的问题是,是否可以编写条件代码,以便 index.js 根据设备是 iPad 还是 iPhone 加载适当的 XML?

我知道这么多代码

index.js

if(Ti.Platform.osname =="iphone")
{
   //Load iPhone xml
}
if(Ti.Platform.osname =="ipad")
{
   //Load iPad xml
}

有人可以帮我解决这个问题吗?任何帮助,将不胜感激。 提前致谢!

【问题讨论】:

    标签: javascript iphone ipad titanium titanium-alloy


    【解决方案1】:

    一个控制器有两个视图可以被认为是没有视图的控制器,你可以在 xml 中使用require 标签,在控制器中使用Alloy.createController('foo').getView();

    这是documentation

    【讨论】:

      【解决方案2】:

      不确定这是最好的方法,但我相信这会实现您想要的。

      var myController = null;
      
      if(Ti.Platform.osname =="iphone")
      {
         myController = Alloy.createController('ios/iPhone/index');
      }
      if(Ti.Platform.osname =="ipad")
      {
         myController = Alloy.createController('ios/iPad/index');
      }
      

      我使用内置属性来允许我使用单个控制器。

      index.xml

      <Alloy>
        <View id="container" formFactor="tablet">
          <Label id="tabletLabel">Tablet</Label>
        </View>
        <View id="container" formFactor="handheld">
          <Label id="phoneLabel">iPhone</Label>
        </View>
      </Alloy>
      

      这只会在适当的外形尺寸内创建元素。因此,如果设备是平板电脑,则在屏幕创建后 phoneLabel 甚至都不存在。这样,您就有了一个控制器来创建两个屏幕,从而允许您重用 index.js 文件中的逻辑功能。这有助于您减少重复代码。

      【讨论】:

      • 代码给出错误找不到模块alloy/controllers/ios/iPhone/index。所以我假设它会为每部 iPhone 和 iPad 搜索相应的控制器。
      • 我说 index.js 应该说 index.xml。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      相关资源
      最近更新 更多