协议代表始终为零

时间:2019-12-20 16:00:22

标签: ios swift uiview delegates xib

我不熟悉xib文件。所以我不清楚他们如何与父母互动。

我有一个自定义视图(LoginView),该视图是从xib文件创建的。该视图还定义了协议(LoginDelegate)。委托的唯一目的是将用户名和密码传递回呼叫者。

我还有一个实现该协议的ViewController(LoginVC)。我正在将LoginView添加到此VC。

我确认已在VC.viewDidLoad()中正确设置了委托。问题是当我尝试使用委托来调用协议方法时:委托始终为nil。不知何故它被清除了。这是UIView:

// MARK:- Login Delegate method
// provide a means to pass the user credentials back to the parent
protocol LoginDelegate: AnyObject {
    func getUsernameAndPassword(user: String, pwd: String)
}

class LoginView: UIView, UITextViewDelegate {

    @IBOutlet weak var user: UITextView!
    @IBOutlet weak var password: UITextView!
    @IBOutlet weak var btnLogin: UIButton!
    var userPlaceholderLabel : UILabel!
    var pwdPlaceholderLabel : UILabel!
    var view: UIView!

    weak var loginDelegate: LoginDelegate?


    // MARK:- internal use only
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    class func getInstance() -> LoginView {
      let nib = UINib(nibName:"LoginView", bundle:nil)
      let view = nib.instantiate(withOwner: self, options: nil).first as! LoginView
      return view
    }

    @IBAction func onLoginButtonPress(_ sender: Any) {
        print ("\(#function): Username: \(user.text ?? ""), Password: \(password.text ?? "")")

        self.loginDelegate?.getUsernameAndPassword(user: user.text, pwd: password.text )
    }

    // MARK:- TextView Delegate methods
    func textViewDidChange(_ textView: UITextView) {...}  
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {...}
}

和视图控制器:

class LoginVC: UIViewController, LoginDelegate {

    var isBleScan = true
    @IBOutlet weak var btnToggleBleScan: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let loginSubview = LoginView.getInstance()
        loginSubview.frame = self.view.bounds
        loginSubview.loginDelegate = self
        view.addSubview(loginSubview)
    }

    @IBAction func onToggleScanPressed(_ sender: Any) {
        isBleScan = !isBleScan
        if (isBleScan) {
            btnToggleBleScan.setTitle("Stop Scan", for: UIControl.State.normal)
        } else {
            btnToggleBleScan.setTitle("Start Scan", for: UIControl.State.normal)
        }
    }

    // MARK:- LoginDelegate methods
// METHOD IS NEVER CALLED - DELEGATE IS nil IN THE CALLER
    func getUsernameAndPassword(user: String, pwd: String) {
        let _user = user
        let _pwd = pwd
        print ("\(#function):Username: \(_user), Password: \(_pwd)")        
    }
}

主要故事板和子xib的连接分别为: Main Storyboard LoginVC (aka Main)

LoginView xib LoginView (aka child) 我怀疑我在IB中接线不正确,但不确定。我找到了有关此问题的许多答案。我尝试了许多建议的解决方案,但没有任何效果。感谢您的投入!

1 个答案:

答案 0 :(得分:1)

创建

  class func getInstance() -> LoginView { 
    let nib = UINib(nibName:"LoginView", bundle:nil)
    let view = nib.instantiate(withOwner: self, options: nil).first as! LoginView
    return view
  }

然后

    let loginSubview = LoginView.getInstance()
    loginSubview.frame = self.view.bounds
    loginSubview.loginDelegate = self   // I verified this sets the delegate to an instance of LoginVC
    view.addSubview(loginSubview)

然后删除此功能loadViewFromNib


当前问题,当您执行LoginView()时会创建一个没有布局的实例并为其设置委托,但是从您的loadViewFromNib中创建另一个具有正确布局的实例并将其添加到该视图中,但这会丢失委托分配,因此您在vc中创建的视图的顶部子视图有一个零委托