gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:37:55 28 4
gpt4 key购买 nike

我有一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com