gpt4 book ai didi

swift - Swift 中带参数的静态函数的选择器?

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:25 24 4
gpt4 key购买 nike

是否可以在 Swift 中使用带参数的静态函数的选择器?

我试过的语法是这样的:

  button.addTarget(VCPresenter.self, action: #selector(VCPresenter.handleBackButton(_:parentNavigationController!)), for: .touchUpInside)

静态方法就是这个

  @objc public static func handleBackButton(_ controller: UINavigationController) {
controller.popViewController(animated: true);
}

当我编译时,我得到

Error:(24, 56) argument of '#selector' does not refer to an '@objc' method, property, or initializer

这甚至可以做到吗?

最佳答案

我用单 View 应用程序和这项工作创建了新项目。如果这对你有用,请告诉我。

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let button = UIButton()
button.addTarget(self, action: #selector(ViewController.buttonAction(x:y:)), for: .touchUpInside)
}

@objc public static func buttonAction(x: Int, y: String){
print("Test")
}
}

关于swift - Swift 中带参数的静态函数的选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648956/

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