当ViewController有一个从Xib唤醒的视图时,NSUnknownKeyException

时间:2017-06-20 11:38:36

标签: ios iphone uiviewcontroller nsunknownkeyexception

我只想使用代码

从ViewController跳转到另一个
    btn_routh.addTarget(self, action: #selector(self.goToPoiDetail), for: .touchUpInside)

    @objc open func goToPoiDetail() {

    self.present(PoiDetailViewController(), animated: true, completion: nil)
}

但是当我点击btn_routh按钮时,出现错误

*** Terminating app due to uncaught exception 'NSUnknownKeyException',
    reason: '[<OMOT.PoiDetailViewController 0x7fb269f1e010> 
    setValue:forUndefinedKey:]: this class is not key value coding-compliant 
    for the key addressLabel.'

但是当我写作

    print("print")
在goToPoiDetail函数中,它打印得很好,所以我认为问题在于跳跃句子

在其他地方

btn_cate.addTarget(self, action: #selector(self.showSubMenu), for: .touchUpInside)

点击btn_cate按钮效果很好。

当前ViewController与其他ViewControllers的最大区别在于ViewController中有一个自定义View,而自定义视图使用xib来构建视图界面。但是没有跳跃动作,自定义视图显示良好。

以下是我的自定义视图的代码,其中有一个字段“addressLabel”,显示错误警告

import UIKit

class PoiDetailView: UIView {
    @IBOutlet weak var categoryLabel: UILabel!
    @IBOutlet weak var introLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var distanceLabel: UILabel!
    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet var containView: UIView!
    var action:(()->Void)?

    var category:String = ""{
        didSet{
            categoryLabel.text = category
        }
    }
    var intro:String = ""{
        didSet{
            introLabel.text = intro
        }
    }
    var address:String = ""{
        didSet{
            addressLabel.text = address
        }
    }
    var distance:String = ""{
        didSet{
            distanceLabel.text = distance
        }
    }
    var imageUrl:String = ""{
        didSet{
            ImageLoader.sharedLoader.imageForUrl(urlString: imageUrl){
                image,url in
                print("load image")
                self.imageView.image = image
            }
        }
    }
    var imageAsset:String = ""{
        didSet{
            imageView = UIImageView(image: UIImage(named: imageAsset))
        }
    }

    func setImage(data:Data){
        print("\(#function)")
        imageView.image = UIImage(data: data)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupXib()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupXib()
    }
    func setupXib(){
        let view = Bundle.main.loadNibNamed("PoiDetailView", owner: self, options: nil)?.first as! UIView
        view.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        addSubview(view)
    }


    @IBAction func closeDetail(_ sender: UIButton) {
        isHidden = true
    }

}

问题出在哪里?如何从这个ViewController跳转到拥有Xib的视图。

1 个答案:

答案 0 :(得分:0)

Check the IBOutletConnection of your addressLabel.enter image description here

better, disconnect the layout connection you have made and connect the label again. You're probably referring to a non-existent IBOutlet or IBAction. Hope it will help.