0

I have a UIView and a ViewController. In the UIView I have a UITextField, and I need to use a delegate so I can dismiss the keyboard by tapping the "Done" button. The problem is that I can't use UITextViewDelegate in the UIView and I can't access the Textfield in the UIView from the ViewController. How can I add a delegate so I can dismiss my textfield?

ViewController:

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(){

}
}

View:

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
}()

1 Answer 1

1

No need you can do

self.view.endEditing(true)

Or

let vi = BaseView()
vi.addCommentTextField.delegate = self
self.view = vi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.