2016-09-21 17 views
7

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.

+0

thông báo lỗi là gì? –

+0

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

Trả lời

8

Trước hết hãy xem vòng đời của chế độ xem. Tùy thuộc vào điều này chúng ta có thể nhấn mạnh rằng phương pháp awakeFromNib là khá phù hợp vì:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

2

Hãy chắc chắn để chấm @IBOutlet cho xem nội dung .Xib, bạn cũng cần phải thêm mã NIB. Cuối cùng, hãy chắc chắn trong ViewController của bạn, bạn đặt UIView Outlet của bạn là UserAlertVC và bạn thêm phương thức awakeFromNib. Vui lòng tìm mã đính kèm. Hãy cho tôi biết nếu bạn cần thêm trợ giúp.

Đây là mã liên quan đến tệp .xib.

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 

} 

Đây là mã liên quan đến ViewController.

class ViewController: UIViewController { 

//MARK: - Outlets 

@IBOutlet weak var userAlertVC: UserAlertVC! 

//MARK: - Loads 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 

} 
Các vấn đề liên quan