2015-09-11 27 views
0

我正在创建一个简单的应用程序来使用文本框和按钮来旋转图像。 (不使用合金)自定义函数的钛按钮监听器

当我在eventListener上捕获事件时,我想使用两个自定义函数,但出现奇怪的错误。

的代码如下

Titanium.UI.setBackgroundColor('#000'); 

var win1 = Titanium.UI.createWindow({ 
    title: "App Rotation", 
    backgroundColor : 'orange' 
}); 


// Create a TextField. 
var textField = Ti.UI.createTextField({ 
    height : 35, 
    top : 10, 
    left : 40, 
    width : 240 
}); 

win1.add(textField); 

// Create an ImageView. 
var image = Ti.UI.createImageView({ 
    image : 'appceleratorIcon.png', 
}); 
// Add to the parent view. 
win1.add(image); 

var current_degrees = 0; 

// Create a Button. 
var button = Ti.UI.createButton({ 
    title : 'Rotate Image', 
    top : '70%', 
    left : '30%' 
}); 

// Listen for click events. 
button.addEventListener('click', function() { 
    var txtfield_value = textField.value; 
    var dialog_text; 
    var dialog_title; 

    if (txtfield_value >= -90 && txtfield_value <= 90) { 

     current_degrees = rotate_degrees(current_degrees, txtfield_value); 
     dialog_text = "Rotated " + txtfield_value; 
     dialog_title = "Rotation completed"; 

    } else { 

     dialog_text = "Type between -90 and 90"; 
     dialog_title = "Error!"; 

    } 
    show_dialog(dialog_text, dialog_title); 
}); 

// Add to the parent view. 
win1.add(button); 

function rotate_degrees(new_degrees, old_degrees) { 

    var matrix2d = Ti.UI.create2DMatrix().rotate(new_degrees); 
    img.transform = matrix2d; 

    return (parseInt(new_degrees) + parseInt(old_degrees)); 
} 

win1.open(); 

function show_dialog (dialog_text, dialog_title) { 
    var dialog = Ti.UI.createAlertDialog({ 
    message: dialog_text, 
    ok: 'Continue', 
    title: dialog_title 
    }); 
    dialog.show(); 
} 

和错误是:

[ERROR] : ReferenceError: anium is not defined 
[ERROR] : File: app.js 
[ERROR] : Line: undefined 
[ERROR] : SourceId: undefined 
[ERROR] : Backtrace: 
[ERROR] : undefined 

我曾尝试注释代码,并不会出现此错误的唯一的一次是当我的评论都自定义功能。

这是编写自定义函数的方式,这样当一个按钮被调用时,我可以执行我的函数,以便程序保持'干净'并且不会被代码弄脏?

在此先感谢。

回答

0

'anium'没有被定义看起来像别处的错字。只要搜索它,也许你会在“Tit anium”之间找到一些空间。

此外,您有一个函数dialog_title,但正在创建一个具有相同名称的本地变量,并且想要使用按钮eventlistener中的函数。