【问题标题】:Protocol method does not pass the correct parameter协议方法没有传递正确的参数
【发布时间】:2021-07-19 05:01:35
【问题描述】:

考虑这种情况,假设您有一个 UIView 的子类和一个协议,即

protocol MyViewProtocol
{
    func didTapSelf(thisView: MyView)
}

class MyView: UIView
{
    lazy var tf: UITextField =
        {
            let obj = UITextField()
            obj.translatesAutoresizingMaskIntoConstraints = false
            obj.placeholder = "Enter text"
            obj.backgroundColor = .white
            return obj
        }()
    
    var btn: UIButton =
        {
            let obj = UIButton()
            obj.translatesAutoresizingMaskIntoConstraints = false
            obj.addTarget(self, action: #selector(didTapBtn), for: .touchUpInside)
            obj.backgroundColor = .orange
            return obj
        }()
    
    var delegate: MyViewProtocol?
    
    init(backgroundColor: UIColor, placeholder: String, delegate: MyViewProtocol?)
    {
        super.init(frame: .zero)
        self.backgroundColor = backgroundColor
        self.delegate = delegate
        tf.placeholder = placeholder
        
        configureUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configureUI()
    {
        addAllSubviews(tf,
                       btn)
        
        NSLayoutConstraint.activate([
            
            btn.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
            btn.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
            btn.topAnchor.constraint(equalTo: topAnchor, constant: 32),
            
            tf.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
            tf.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
            tf.topAnchor.constraint(equalTo: btn.bottomAnchor, constant: 12),
            tf.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
            tf.heightAnchor.constraint(equalToConstant: 44)
        ])
        
//        let gesture = UITapGestureRecognizer(target: self, action: #selector(didTapBtn))
//        gesture.cancelsTouchesInView = false
//        addGestureRecognizer(gesture)
    }
    
    @objc func didTapBtn()
    {
        delegate?.didTapSelf(thisView: self)
    }
}

现在,在您的视图控制器中,创建 2 个 MyView 实例,即

lazy var myView1: MyView =
        {
            let obj = MyView(backgroundColor: .yellow, placeholder: "view 1", delegate: self)
            obj.translatesAutoresizingMaskIntoConstraints = false
            return obj
        }()
    
    lazy var myView2: MyView =
        {
            let obj = MyView(backgroundColor: .green, placeholder: "view 2", delegate: self)
            obj.translatesAutoresizingMaskIntoConstraints = false
            return obj
        }()

并使用如下协议方法实现您的视图控制器:

extension ViewController: MyViewProtocol
{
    func didTapSelf(thisView: MyView)
    {
        if thisView == myView1
        {
            print("tapped view 1")
        }
        else if thisView == myView2
        {
            print("tapped view 2")
        }
        else
        {
            print("i am ambigious")
        }
    }
}

现在,当您点击 myView1myView2 中的按钮时,它将正确打印,即点击视图 1 或 2,无论点击哪个按钮,但是当您开始编辑 myView1 文本字段时在编辑状态下,无论你点击myView1myView2中的哪个按钮,它都只会打印被点击的视图1,或者任何其textField处于编辑模式的视图。

据我所知,无论 textField 的编辑状态如何,它都必须传递正确的 self 对象。为什么会出现这种行为?

p.s 当您在上述任何视图上使用点击手势时,不会发生此行为。

【问题讨论】:

    标签: ios swift uibutton uitextfield


    【解决方案1】:

    这实际上与协议或故事中的任何其他红鲱鱼无关。稍加调试就会发现,实际上点击了正确的按钮,但运行时实际上是在将目标-操作消息发送到错误的 MyView。根本原因是在这一行:

    obj.addTarget(self, action: #selector(didTapBtn), for: .touchUpInside)
    

    ...self 这个词并不代表你认为的那样。

    要解决问题,只需更改

    var btn: UIButton =
    

    lazy var btn: UIButton =
    

    【讨论】:

    • 就是这样!这只是我抱怨here 的错误的又一次发生。
    • 谢谢马特。在添加目标到按钮时, self 未初始化。这会向稍后会看到此内容的人添加信息。作为建议,swift 应该给出一个 self not available 的编译时错误。我想知道为什么它没有。
    • 是的,这就是我的咆哮。这是一个非常有趣的案例!
    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多