LoginSignup
1
1

More than 5 years have passed since last update.

TitaniumにおけるWindow間のデータの受け渡し

Posted at

Window間のデータの受け渡しには以下の二つの方法がある

  • 参照渡し
  • イベント

参照渡し

生成したウインドウのプロパティー(myAlertMessage)にメッセージ変数の参照を保存

app.js
var message = "Hi world!";
var w = Titanium.UI.createWindow({ url: "foo.js" });
w.myAlertMessage = message;

currentWindowを経由して、設定されたmyAlertMessageを参照できる

foo.js
alert(Titanium.UI.currentWindow.myAlertMessage);

イベント

データを受け取りたいWindow上でカスタムイベント(下の例ではfooというイベント)に対するイベントハンドラを定義

bar.js
Titanium.UI.currentWindow.addEventListener('foo',function(e)
{
  Titanium.API.info("foo event received = "+JSON.stringify(e));
});

データを送信する側からfireEvent()でイベントを発火し、別Windowにイベントハンドラに経由でデータ(イベントハンドラの引数として)を渡すことができる

app.js
var window = Titanium.UI.createWindow({
  url:'bar.js'
});
window.open();
window.fireEvent('foo',{a:'b'});

Reference

Titanium.UI.Window
http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.UI.Window

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1