javascript - 使用 Cordova Webview/Phonegap 按钮 onclick 更改 native View (事件监听器)

标签 javascript objective-c events cordova

我在 native 应用程序中使用 Cordova。当用户在我的服务器上完成上传关卡后,他可以选择下一步要做的事情。现在,他应该能够通过单击“立即开始关卡!”按钮,直接从 Cordova Webviewer 中开始游戏。

新关卡将被下载并在另一个 ViewController 中启动。

如何在我的 native 代码中监听按钮的 onclick 事件以在 PlayLevelVievcontroller.m 上执行 Segue?

最佳答案

您需要编写一个插件。

然后打电话 onclick="plugin.callNative(['arguments'])"

然后 Cordova 调用你自己的本地类。

var myplugin = {
    performSegue: function (arguments) {
        var callback = function () {};
        cordova.exec(callback, callback, "nativeClass", "nativeMethod",arguments);
    }
};

并像这样声明您的 native 类和方法

@interface MyPlugin : CDVPlugin

- (void)myNativeMethod:(CDVInvokedUrlCommand *)urlCommand;

@end

并像这样实现你的 native 类

- (void)myNativeMethod:(CDVInvokedUrlCommand *)urlCommand
{
    CDVPluginResult* pluginResult = nil;
    NSArray *arguments = urlCommand.arguments;
if (Arguments are not right) {

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];

} else {
    // Do something
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:urlCommand.callbackId];

}

关于javascript - 使用 Cordova Webview/Phonegap 按钮 onclick 更改 native View (事件监听器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926905/

相关文章:

javascript - JavaScript 中的 Blob 对象转为 base64

javascript - JavaScript 中的这是什么?

javascript - 将正则表达式 "."放在文本框的按键上

更改布局的 Javascript/jQuery focusout 事件导致 click 事件不触发

mysql - 为什么 mysql 事件计划在数据库 localhost 中不起作用?

javascript - 允许 Gridstack 仅可拖动到项目的特定区域

javascript - 显示我使用 @ContentChildren 或 @ViewChild 获取的组件的内容

iphone - 如何使用包含彩色图像的按钮自定义 UIToolbar?

iphone - 从代码中更改了 UIImageView 的图像,不再居中

iphone - 从屏幕上删除键盘而不调用 resignFirstResponder