当我将它传递给父视图时,无法识别的选择器发送到实例

Unrecognized selector sent to instance when I'm passing it to parent view

我需要对 navigationItemBar 进行一些更改并添加具有不同对象的按钮。

这是我添加选择器的 viewDidLoad 函数代码

    override func viewDidLoad() {
    super.viewDidLoad()
    
    tapGestureRecgonizer.addTarget(self, action: #selector(showAlertSheet))
    imageView.addGestureRecognizer(tapGestureRecgonizer)
    
    setupAlert()
    
    self.tabBarController?.setLeftNavBarView(selector: #selector(self.returnToGalleryScreenTab))
    self.tabBarController?.setRightNavBarView(selector: #selector(self.routeToAddPhotoForm))
}

这是我的@objc 函数

    @objc func routeToAddPhotoForm() {
    self.presenter?.routeToAddPhotoForm()
}

@objc func returnToGalleryScreenTab() {
    self.tabBarController?.selectedIndex = 0
}

以及我在导航栏项目中设置视图的扩展

    func setLeftNavBarView(selector: Selector) {
    let button = UIButton(type: .custom)
    button.setTitle("Cancel", for: .normal)
    button.setTitleColor(UIColor(rgb: 0xFF5F5F5F), for: .normal)
    button.titleLabel?.font = UIFont(name: "Roboto", size: 15)
    button.addTarget(self, action: selector, for: .touchUpInside)
    button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    let barButton = UIBarButtonItem(customView: button)
    navigationItem.leftBarButtonItem = barButton
}

func setRightNavBarView(selector: Selector) {
    let button = UIButton(type: .custom)
    button.setTitle("Next", for: .normal)
    button.setTitleColor(UIColor(rgb: 0xFFCF497E), for: .normal)
    button.titleLabel?.font = UIFont(name: "Roboto-Bold", size: 15)
    button.addTarget(self, action: selector, for: .touchUpInside)
    button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    let barButton = UIBarButtonItem(customView: button)
    navigationItem.rightBarButtonItem = barButton
}

但是当我尝试点击 NavigationBarItem 中的按钮时,我收到类似“'-[UITabBarController returnToGalleryScreenTab]:无法识别的选择器发送到实例 0x13f00fa00'” 的错误。我究竟做错了什么。我认为我的问题是我正在将我的@objc 函数传递给父视图函数,但我不知道如何以其他方式做到这一点

下面两个函数-

@objc func routeToAddPhotoForm() {
}

@objc func returnToGalleryScreenTab() {
}

应该在你的 UITabBarController subclass.

原因是 - self 指向你的 UITabBarController subclass - 而不是你调用它的另一个 class。

button.addTarget(self, action: selector, for: .touchUpInside)

另一种方法是遵循 -

func setLeftNavBarView(target: Any, selector: Selector) {
    // Use `target` instead of `self` here
    button.addTarget(target, action: selector, for: .touchUpInside)
}

func setRightNavBarView(target: Any, selector: Selector) {
    // Use `target` instead of `self` here
    button.addTarget(target, action: selector, for: .touchUpInside)
}

来自调用站点 -

self.tabBarController?.setLeftNavBarView(target: self, selector: #selector(self.returnToGalleryScreenTab))
self.tabBarController?.setRightNavBarView(target: self, selector: #selector(self.routeToAddPhotoForm))