Lỗi xảy ra khi tôi đặt đại biểu UITextField.Nơi đặt phương thức ủy nhiệm UiTextField trong tùy chỉnh UiView
Mã của tôi là:
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)
}
}
Thông báo lỗi là:
fatal error: unexpectedly found nil while unwrapping an Optional value
Tôi đã sử dụng Tuỳ chỉnh Alert Xem bằng XIB.pls đề nghị bất kỳ giải pháp.
thông báo lỗi là gì? –
lỗi nghiêm trọng: bất ngờ tìm thấy nil trong khi unwrapping một giá trị tùy chọn – Gaurav