swift - 在 View 和 View Controller 之间委托(delegate)

标签 swift delegates

我有一个 UIView 和一个 ViewController。 在 UIView 中,我有一个 UITextField,我需要使用一个委托(delegate),这样我就可以通过点击“完成”按钮来关闭键盘。 问题是我无法在 UIView 中使用 UITextViewDelegate 并且无法从 访问 UIView 中的 Textfield View Controller 。 如何添加委托(delegate)以便我可以关闭我的文本字段?

View Controller :

import UIKit


class LoginVC: UIViewController, UITextViewDelegate {
override func loadView() {
    super.loadView()
    self.view = BaseView()
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.tabBarController?.navigationController?.setNavigationBarHidden(true, animated: false)

    navigationItem.title = "LoginVC"
    navigationController?.navigationBar.barTintColor = UIColor.appBlue
    navigationController?.navigationBar.barStyle = .black
    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.appWhite]
    navigationItem.rightBarButtonItem?.tintColor = UIColor.appWhite

    let btnImage = UIImageView(image: UIImage(named: "Help"))
    btnImage.changeImageColor(color: UIColor.appWhite)

    //TODO: Change the btnImage to color white!
    let helpButton = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    helpButton.setImage(btnImage.image, for: .normal)
    helpButton.addTarget(self, action: #selector(showHelpPopup), for: .touchUpInside)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: helpButton)

}

@objc func showHelpPopup(){

}
}

查看:

import UIKit

public final class BaseView: UIView, UITextFieldDelegate {
public override init(frame: CGRect) {
    super.init(frame: frame)
    self.addSubview(masterStackView)

    self.backgroundColor = UIColor.white
    NSLayoutConstraint.activate([
        masterStackView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
        masterStackView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
        masterStackView.leadingAnchor.constraint(lessThanOrEqualToSystemSpacingAfter: self.leadingAnchor, multiplier: 0.2),
        masterStackView.trailingAnchor.constraint(lessThanOrEqualToSystemSpacingAfter: self.trailingAnchor, multiplier: -0.2),
        masterStackView.heightAnchor.constraint(equalToConstant: 100),
    ])
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

public let addCommentTextField: UITextField = {
    let text = UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 30))

    text.placeholder = "Comment"
    text.font = UIFont.systemFont(ofSize: 14, weight: .bold)
    text.borderStyle = UITextField.BorderStyle.roundedRect
    text.autocorrectionType = UITextAutocorrectionType.no
    text.keyboardType = UIKeyboardType.default
    text.returnKeyType = UIReturnKeyType.done
    text.clearButtonMode = .always
    text.contentHorizontalAlignment = .left

    return text
}()

最佳答案

不用你做

self.view.endEditing(true)

或者

let vi = BaseView()
vi.addCommentTextField.delegate = self
self.view = vi

关于swift - 在 View 和 View Controller 之间委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56384798/

相关文章:

ios - 处理任务过程中中断(例如电话、电池警告)的最佳方法

ios - 有没有办法在 swift 4 中更改 UINavigationBar 的背景颜色

ios - 在任何地方点击导航项中的搜索 Controller 的键盘关闭

swift - 使用 Coco/R 翻译源代码后如何测试输入条件?

swift - 将 cookie 传递给 WKWebView/SFSafariViewController?

ios - viewDidLoad 中的标签没有更新?

ios - 更改 MoreNavigationController TableView Row 中的角标(Badge)值文本和背景颜色?

ios - 标签栏 : interaction between tabs

objective-c - 在发送消息之前检查有效的委托(delegate)对象

iOS 通过委托(delegate)关闭 View Controller