Where to set UiTextField delegate method in custom UiView

The error occurs when I install the UITextField delegate.

My code is:

import UIKit class UserAlertVC: UIView , UITextFieldDelegate { /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ override init(frame: CGRect) { super.init(frame: frame) } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! self.addBehavior() } func addBehavior (){ print("Add all the behavior here") userNameTxtField.delegate = self passwordTxtField.delegate = self } func textFieldShouldReturn(textField: UITextField) -> Bool { return true } func textFieldDidBeginEditing(textField: UITextField) { } @available(tvOS 10.0, *) func textFieldDidEndEditing(textField: UITextField, reason: UITextFieldDidEndEditingReason) { } @IBAction func actionOnCancel(sender: UIButton) { self .removeFromSuperview() } @IBAction func actionOnProceed(sender: UIButton) { self .removeFromSuperview() UserAlertVC.showAlertForUser() } @IBOutlet var userNameTxtField: UITextField! @IBOutlet var passwordTxtField: UITextField! static func showAlertForUser() { let alert = NSBundle.mainBundle().loadNibNamed("KeyboardViewController", owner: self, options: nil)!.last as! UIView let windows = UIApplication.sharedApplication().windows let lastWindow = windows.last alert.frame = UIScreen.mainScreen().bounds lastWindow?.addSubview(alert) } } 

Error message:

 fatal error: unexpectedly found nil while unwrapping an Optional value 

I used Custom Alert View using XIB.pls, offering any solution.

+7
tvos swift uitextfielddelegate
source share
2 answers

First take a look at the life cycle of a view. Depending on this, it can be distinguished that the awakeFromNib method awakeFromNib quite suitable, because:

The nib-load infrastructure sends an awakeFromNib message to each object recreated from the nib archive, but only after all objects in the archive are loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed that it has all the actions already installed.

+8
source share

Be sure to put @IBOutlet for the contents of the .Xib content, also you need to add the Nib code. Finally, make sure that in your ViewController you set the UIView Outlet to UserAlertVC and add the awakeFromNib method. Attach the code. Let me know if you need more help.

Here is the code associated with the .xib file.

 import UIKit class UserAlertVC: UIView, UITextFieldDelegate { //MARK: - Outlets @IBOutlet var contentView: UIView! @IBOutlet var userNameTxtField: UITextField! @IBOutlet var passwordTxtField: UITextField! //MARK: - Loads override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! commonInit() } //MARK: - Functions func commonInit() { Bundle.main.loadNibNamed("UserAlertVC", owner: self, options: nil) userNameTxtField.delegate = self passwordTxtField.delegate = self contentView.translatesAutoresizingMaskIntoConstraints = false addSubview(contentView) // add constraints programmatically } // add the rest of your code } 

Here is the code related to ViewController.

 class ViewController: UIViewController { //MARK: - Outlets @IBOutlet weak var userAlertVC: UserAlertVC! //MARK: - Loads override func viewDidLoad() { super.viewDidLoad() } override func awakeFromNib() { super.awakeFromNib() } } 
+2
source share

All Articles