2014-11-05 69 views
0

我使用钛合金MVC与需要扫描QR码 钛SDK 3.4.0.GA返回结果从另一个控制器钛合金

项目控制器我有2个控制器:index.js和secondwindow。 js和它们各自的视图index.xml和secondwindow.xml。 我要开始扫描并处理secondWindow控制器扫描的结果,而结果返回到索引控制器,让索引处理自己的UI元素

我试着像这样 INDEX.XML:

<Alloy> 
    <Window> 
    <Label id='result' /> 
    ...Other components... 
    <Button onClick='startScan'>Start QR scan</Button> 
    </Window> 
</Alloy> 

index.js:

function whenSecondWindowFinish(arg){ 
    //update index.xml 
    $.result.setText(arg); 
} 

function startScan(e){ 
    Alloy.createController('secondWindow'); 
} 

$.index.open(); 



secondWindow.xml:

<Alloy> 
    <Window exitOnClose='false'> 
    </Window> 
</Alloy> 

secondWindow.js:

function scanOK(data){ 
var returnResult = /*Handle data*/ 
//I need to return the result to the index controller 
$.secondWindow.close();//And close this view 
} 

function canceled(){ 
//return {} to index controller 
$.secondWindow.close();//And close this view 
} 

var QRscanner = require('qrscanner'); 
var qroptions = { 
    //width height ... 
    success: scanOK, 
    cancel: canceled 
}; 
var qrview = QRscanner.createQRView(qroptions); 
$.secondWindow.add(qrview); 
$.secondWindow.open(); 


我怎样才能成功关闭此窗口/取消功能,并将结果返回到索引控制器或通知索引执行whenSecondWindowFinish扫描(/ 通ARG结果 /);方法?或者哪种方法是正确的?

+0

我了解的是你想从'secondWindow.js'发送一些结果'index.js'(对吗?)和你想发送的结果是什么类型(String,object,other)? – turtle 2014-11-06 03:32:15

回答

1

使用回调。

index.js:

var callbackFunc = function(data){ 
    //do something with the data variable 
} 
Alloy.createController('secondwindow', {'callback':callbackFunc}); 

secondwindow.js:

var args = arguments[0] || {}; 

function scanOK(data){ 
    args.callback(data) 
//I need to return the result to the index controller 
$.secondWindow.close();//And close this view 
} 

你也可以使用一个Ti.App.fireEvent得到同样的事情,但在这里就是为什么你不应该:http://www.tidev.io/2014/09/10/the-case-against-ti-app-fireevent-2/ (哦,该链接也解释了回调:)

0

使用这一个: -

activity.startActivityForResult(intent, function(e) { 
    // The request code used to start this Activity 
    var requestCode = e.requestCode; 
    // The result code returned from the activity 
    // (http://developer.android.com/reference/android/app/Activity.html#StartingActivities) 
    var resultCode = e.resultCode; 
    // A Titanium.Android.Intent filled with data returned from the Activity 
    var intent = e.intent; 
    // The Activity the received the result 
    var source = e.source; 
}); 

OR

Have a look here

斯巴达克斯谢谢:)

+0

谢谢,但我需要的代码与iOS和Android一起工作,您的代码仅适用于Android,我想我会使用回调作为参数方法 – RPAZ 2014-11-11 16:27:15