posts - 11,  comments - 21,  trackbacks - 0

问题: 以下代码,tab1的click事件在Android中生效,在iOS不生效

   1:  function ApplicationTabGroup(Window) {
   2:      //create module instance
   3:      var self = Ti.UI.createTabGroup();
   4:   
   5:      //create app tabs
   6:      var win1 = new Window(L('home')), win2 = new Window(L('settings'));
   7:   
   8:      var tab1 = Ti.UI.createTab({
   9:          title : L('home'),
  10:          icon : '/images/KS_nav_ui.png',
  11:          window : win1
  12:      });
  13:      win1.containingTab = tab1;
  14:   
  15:      var tab2 = Ti.UI.createTab({
  16:          title : L('settings'),
  17:          icon : '/images/KS_nav_views.png',
  18:          window : win2
  19:      });
  20:      win2.containingTab = tab2;
  21:   
  22:      self.addTab(tab1);
  23:      self.addTab(tab2);
  24:   
  25:      tab1.addEventListener('click',function(){
  26:          //这个事件在iOS中不会被触发
  27:      });
  28:   
  29:      return self;
  30:  };
  31:   
  32:  module.exports = ApplicationTabGroup;

解决方案:

通过看Titanium附带的示例程序 Kitcken Sink , 找到了解决方案.

为TabGroup添加focus事件,然后对事件参数进行判断,来确定当前被点击的是那个tab.

 

   1:  function ApplicationTabGroup(Window) {
   2:      //create module instance
   3:      var self = Ti.UI.createTabGroup();
   4:   
   5:      //create app tabs
   6:      var win1 = new Window(L('home')), win2 = new Window(L('settings'));
   7:   
   8:      var tab1 = Ti.UI.createTab({
   9:          title : L('home'),
  10:          icon : '/images/KS_nav_ui.png',
  11:          window : win1
  12:      });
  13:      win1.containingTab = tab1;
  14:   
  15:      var tab2 = Ti.UI.createTab({
  16:          title : L('settings'),
  17:          icon : '/images/KS_nav_views.png',
  18:          window : win2
  19:      });
  20:      win2.containingTab = tab2;
  21:   
  22:      self.addTab(tab1);
  23:      self.addTab(tab2);
  24:   
  25:      self.addEventListener('focus', function(e) {
  26:   
  27:          var info = Titanium.API.info;
  28:   
  29:          // 在iOS中, e.source 是 TabGroup对象,
  30:          // 在Android中,e.source 是 Tab对象
  31:          var src = e.source;
  32:          var tab = e.tab;
  33:          var preTab = e.previousIndex;
  34:   
  35:          // e.tab 是当前获得焦点的tab
  36:          // e.index 当前获得焦点的tab的索引,首次为-1
  37:          // e.previousTab 上个tab
  38:          // e.previousIndex 上个tab的索引,首次为null
  39:   
  40:          // On iOS, the "More..." tab is actually a tab container, not a tab. When it is clicked, e.tab is undefined.
  41:          if (!tab) {
  42:              info('在iOS中点击了"More..."');
  43:              return;
  44:          }
  45:   
  46:          // 首次
  47:          if (!preTab) {
  48:              info('首次进入');
  49:              return;
  50:          }
  51:   
  52:          if (tab === tab1) {
  53:              info('点击了tab1');
  54:          } else if (tab === tab2) {
  55:              info('点击了tab2');
  56:          }
  57:      });
  58:   
  59:      return self;
  60:  };
  61:   
  62:  module.exports = ApplicationTabGroup;
posted on 2013-03-31 02:54 xmlspy 阅读(231) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜