如何用HYBRID移动应用程序(iOS)中的notification.confirm替换window.confirm

时间:2015-01-20 18:41:52

标签: ios angularjs cordova hybrid-mobile-app

目前在我的Web应用程序中我正在执行如下面的代码所示。 那么到达$ window后会发生什么。确认编译器在Alert框上等待响应,并依赖于 Leave 获得true或false。 最后,此方法将该布尔值返回到其耦合方法。

// **在WEB APP ** //

function shouldLeave(next) 
                var message =  'Do you wish to leave this Page';
                var leave =  $window.confirm(message); // Return bool value as per user selection

                if (leave) {
                    //Doing my job...
                    reset();
                }
                return leave;
            }

现在混合动力我正在做的是:

function shouldLeave(next) {
                var message =  'Do you wish to leave this Page';
                var leave =  notification.confirm(message, callbackMethod(),title,[Ok, Cancel]); // Here there is no return value . Return depends on Callback as the callback method gets called depending on user selection

                if (leave) {
                    //Doing my job...
                    reset();
                }
                return leave;
            }

function CallBack(index)
{
switch(index)
{
case 1 :
leave=true; break;
case 2 :
leave=false; break;
default:
leave=-1; break
}
return leave;

}

所以在执行Notification.confirm之后,编译器不会等待用户响应并转移到Next行。(但对于Window。确认是这样做的)。 所以现在我的疑问是如何重构这个代码,以便mu应该离开方法将返回正确的离开值到它的耦合方法。因为当混合应用程序中的用户交互 shouldLeave 完成其执行后,我的回调方法执行。所以它的行为不像$ window.confirm功能。

任何建议都表示赞赏。

2 个答案:

答案 0 :(得分:0)

检查

function shouldLeave() {
        var message =  'Do you wish to leave this Page';
        navigator.notification.confirm(
            message, // message
            onConfirm,                       // callback to invoke with index of button
            'Confirmation',                  // title
            'Ok, Cancel'                  // buttonLabels
        );
    }


    //on button press, the onConfirm function is called
    function onConfirm(button) {
        //console.log('You selected button ' + button);
        if(button == 1){
            //pressed "Ok"
            console.log("Ok ACTION");
        }
        else if(button == 2){
            //pressed "Cancel"
            console.log("Cancel ACTION");
        }
    }

这应该有用。

答案 1 :(得分:0)

变化

notification.confirm(message, callbackMethod(), title, [Ok, Cancel]); 

notification.confirm(message, callbackMethod, title, [Ok, Cancel]); 

说明: 如果你放callbackMethod(),它会在执行到达该行时立即执行 如果你将callbackMethod放在()而不是(),则表示要在回调上执行该函数。