在 Phonegap 应用程序上使用 Swift 获取当前 UIViewController

Getting current UIViewController using Swift on a Phonegap app

我是 iOS 应用程序开发的新手,我正在考虑在 Cordova 应用程序上使用 Swift。

我目前正在编写一个需要当前 UIViewController 才能使用 UIAlertController 的插件。

由于需要视图控制器来显示 UIAlertController 实例,如果是 Cordova 应用程序,我该如何获取它?或者我是否必须创建一个新的 UIViewController 实例?

这是我正在使用的代码片段:

import Foundation

@objc(ActionSheet) class ActionSheet : CDVPlugin {
  func show(command: CDVInvokedUrlCommand) {
    var message = command.arguments[0] as String

    let alertController = UIAlertController(title: "Hey AppCoda", message: "What do you want to do?", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    // Where to get viewController?

    viewController.presentViewController(alertController, animated: true, completion: nil)

  }
}

提前致谢。

CDVPlugin 有一个 viewController 属性,因此您的 ActionSheet class 可以访问它。您的代码对我来说工作正常。这是我 运行 它并从 js.

调用 show 方法时看到的

你有什么问题吗?