gpt4 book ai didi

ios - 如何检测 PKAddPassesViewController 中的添加按钮按下

转载 作者:行者123 更新时间:2023-11-28 10:28:08 24 4
gpt4 key购买 nike

我正在尝试检测用户何时点击 PKAddPassesViewController 中的“添加”按钮。

enter image description here

我添加了 addPassesViewControllerDidFinish(),这样当 passVC 被关闭时,函数 addPassesViewControllerDidFinish() 将被调用。


override func viewDidLoad() {
self.pass = try PKPass(data: downloadedData! as Data)
let passVC = PKAddPassesViewController(pass: self.pass)
self.present(passVC!, animated: true)
// when passVC is dimissed by the user, addPassesViewControllerDidFinish is expected to be called, but it never gets called.

}

func addPassesViewControllerDidFinish(_ controller: PKAddPassesViewController) {
print("enter DidFinish")
let passLib = PKPassLibrary()

// Get your pass
guard let pass = self.pass else { return }

if passLib.containsPass(pass) {
print("if start")

// Show alert message for example
let alertController = UIAlertController(title: "", message: "Successfully added to Wallet", preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
controller.dismiss(animated: true, completion: nil)
}))

controller.show(alertController, sender: nil)
print("if end")

} else {
// Cancel button pressed
print("else start");
controller.dismiss(animated: true, completion: nil)
print("else end");
}
}

但是,当用户取消 passVC 时,func addPassesViewControllerDidFinish() 根本不会被调用。

最佳答案

您应该修复三件事:
1. 在您的 ViewController 类中扩展 PKAddPassesViewControllerDelegate。
2. 仔细检查您是否将 delegate 添加到 PKAddPassesViewController:VC?.delegate = self,这会将您的委托(delegate)链接到 PKAddPassesViewControllerDelegate。
3. 在 addPassesViewControllerDidFinish 中,首先关闭 controller。然后在 passLib.containPass 中做任何你想做的事。 alertController 不再属于 controller,可能属于它的父 View 。

关于ios - 如何检测 PKAddPassesViewController 中的添加按钮按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com