Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用创建自定义UIView时出现问题,使用@IBOutlet时发现无Xib错误_Ios_Swift - Fatal编程技术网

Ios 使用创建自定义UIView时出现问题,使用@IBOutlet时发现无Xib错误

Ios 使用创建自定义UIView时出现问题,使用@IBOutlet时发现无Xib错误,ios,swift,Ios,Swift,在第“contentLabel.text=content”行找到nil时出错 打开/关闭commonInit class SuccessfulView: UIView{ @IBOutlet weak var contentLabel: UILabel! convenience init(content:String ,frame: CGRect){ self.init(frame:frame) contentLabel.text = content } r

在第“contentLabel.text=content”行找到nil时出错

打开/关闭commonInit

class SuccessfulView: UIView{

  @IBOutlet weak var contentLabel: UILabel!

  convenience init(content:String ,frame: CGRect){
    self.init(frame:frame)
    contentLabel.text = content

  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)



  }
  override init(frame: CGRect) {
    super.init(frame: frame) // calls designated initializer

  }



  func commonInit(){
    Bundle.main.loadNibNamed("SuccessfulFooterContentView", owner: self, options: nil)
    addSubview(contentLabel)
  }

}

创建具有xib的UIView的子类问题是,如果调用
UIView的
init(frame:)
,它将不会由界面生成器(脚本/xib)初始化

因此,您应该使用Xib初始化视图。就像-

var-content:String=“”
静态公共func实例(内容:String,框架:CGRect)->SuccessfulView?{
让view=Bundle.main.loadNibNamed(“SuccessfulFooterContentView”,所有者:self,选项:nil)作为?SuccessfulView
视图?.frame=frame
//并保存您的内容
查看?.content=内容
返回视图
}
然后在awakeFromNib函数中设置内容

重写函数awakeFromNib(){
super.awakeFromNib()
contentLabel.text=内容
}
你在说什么

Bundle.main.loadNibNamed("SuccessfulFooterContentView", owner: self, options: nil)
相信这会导致这个变量

@IBOutlet weak var contentLabel: UILabel!

待设定。但是,除非nib中的文件所有者设置为SuccessfulView类,并且连接了
contentLabel
outlet,否则它将不会被激活。

如何创建此视图的实例?为了绑定outlet,您需要在视图控制器内实例化实例,该视图控制器本身正在从NIB或故事板场景加载,并且您不会从init访问它。outlet尚未在init()上初始化。您应该在awakeFromNib上设置contentLabel.text。您是否将标签连接到情节提要?