2016-09-21 84 views
7

当我设置UITextField委托时发生错误。在哪里设置UiTextField委托方法在自定义UiView中

我的代码是:

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) 


} 
} 

错误消息:

fatal error: unexpectedly found nil while unwrapping an Optional value 

我一直在使用XIB.pls使用自定义警报视图提出任何解决方案。

+0

什么是错误信息? –

+0

致命错误:在解包可选值时意外发现nil – Gaurav

回答

8

首先看看视图的生命周期。根据这一点,可以强调方法awakeFromNib是相当合适的,因为:

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

确保把一个@IBOutlet为的.xib内容来看,还需要添加笔尖代码。最后,确保在您的ViewController中将您的UIView Outlet设置为UserAlertVC,并添加awakeFromNib方法。请查找附上的代码。让我知道你是否需要进一步的帮助。

这里是与.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 

} 

这里是与ViewController相关的代码。

class ViewController: UIViewController { 

//MARK: - Outlets 

@IBOutlet weak var userAlertVC: UserAlertVC! 

//MARK: - Loads 

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

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

} 
相关问题