問題: 以下代碼,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 閱讀(239)
評論(0) 編輯 收藏