我只想使用代码从 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与其他ViewController的最大区别在于ViewController中有一个自定义View,自定义View使用xib构建view的界面。但是没有跳跃动作,自定义视图显示得很好。
下面是我的自定义视图的代码,其中有一个“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 的视图。