【问题标题】:ios - Custom delegate method is not triggeredios - 未触发自定义委托方法
【发布时间】:2018-11-13 19:26:52
【问题描述】:

我使用以下代码在 TabBarController 中添加了一个圆形按钮

import UIKit

protocol AddButtonProtocol: class {
   func addButtonIsClicked()
}

 class MainTabBar: UITabBar {

     open var buttonDelegate: AddButtonProtocol?
    private var middleButton = UIButton()

    override open func awakeFromNib() {
        super.awakeFromNib()
        setupMiddleButton()
    }

    override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if self.isHidden {
            return super.hitTest(point, with: event)
        }

        let from = point
        let to = middleButton.center

        return sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)) <= 39 ? middleButton : super.hitTest(point, with: event)
    }

    func setupMiddleButton() {
        middleButton.frame.size = CGSize(width: 70, height: 70)
        middleButton.backgroundColor = .blue
        middleButton.layer.cornerRadius = 35
        middleButton.layer.masksToBounds = true
        middleButton.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 0)
        middleButton.addTarget(self, action: #selector(test), for: .touchUpInside)
        addSubview(middleButton)
    }

    @objc func test() {
        print("add button is clicked")
        buttonDelegate?.addButtonIsClicked()

    }
}

看起来像这样。

现在,每当我点击该按钮时,HomeScreenVCViewController 中的方法都会被触发。所以我已经实现了这样的协议和委托。

import UIKit

class HomeScreenVCViewController: UIViewController {
    var addButtonDelegate: MainTabBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        addButtonDelegate?.buttonDelegate = self
    }

}

extension HomeScreenVCViewController: AddButtonProtocol {
    func addButtonIsClicked() {
        print("protocol is working") // Not getting called
    }
}

但它根本不起作用,当我点击那个圆形按钮时,addButtonIsClicked 方法没有被触发。任何帮助将不胜感激。

【问题讨论】:

  • 看来addButtonDelegatenil
  • 是的,它是 nil..
  • 检查已编辑的答案。

标签: ios swift delegates uitabbarcontroller protocols


【解决方案1】:

试试这个代码:

override func viewDidLoad() {
   super.viewDidLoad()
   (tabBarController.tabBar as? MainTabBar)?.buttonDelegate = self
}

【讨论】:

  • 太棒了。非常感谢。
【解决方案2】:

代表没有被调用 b'coz addButtonDelegate 为零。你没有初始化addButtonDelegate 所以addButtonDelegate 对象是nil

解决方案

您需要将addButtonDelegate 设为@IBOutlet,或者您需要在viewDidLoad: 方法中对其进行初始化。

那么只有委托会被调用。

示例 1

@IBOutlet weak var addButtonDelegate: MainTabBar!

在 viewDidLoad 中

addButtonDelegate.buttonDelegate = self

示例 2

创建类似的属性

var addButtonDelegate: MainTabBar?

在 viewDidLoad 中初始化addButtonDelegate

addButtonDelegate = MainTabBar() //or any other initialiser
addButtonDelegate?.buttonDelegate = self

【讨论】:

  • 已经尝试过您的示例 2。但没有成功。现在示例 1 也不起作用,兄弟。
  • 在 ex 1 中,您是否已将 iboutlet 连接到情节提要或 xib 中的 MainTabBar?
【解决方案3】:

您没有在HomeScreenVCViewController 中初始化MainTabBar。这就是为什么addButtonDelegatenil

var addButtonDelegate: MainTabBar = MainTabBar()
    override func viewDidLoad() {
        super.viewDidLoad()
        addButtonDelegate?.buttonDelegate = self
    }

【讨论】:

  • 很遗憾兄弟已经试过了。它也没有用。很抱歉,我应该在帖子中提及我所有的尝试。
  • 您是否调试过addButtonDelegate 变为 nil 的位置?
  • 协议和委托对象在 MainTabBar 中定义得很好,为什么在 HomeScreenVCViewController 中尝试初始化该委托对象时它应该为 nil。我真的找不到为什么它是 nil。
猜你喜欢
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2011-06-18
相关资源
最近更新 更多