【问题标题】:While loading subview from xib to UIScrollView getting error 'EXC_BAD_ACCESS' in swift在将子视图从 xib 加载到 UIScrollView 时,在 swift 中出现错误“EXC_BAD_ACCESS”
【发布时间】:2017-01-28 09:55:52
【问题描述】:

我正在尝试从 xib 加载自定义 UIView

customView.swift

导入 UIKit

@IBDesignable class customView: UIView {

var view: UIView!

override init(frame: CGRect) {
    super.init(frame: frame)

    xibSetup()
}

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

    xibSetup()
}


func xibSetup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

    addSubview(view)
}

func loadViewFromNib() -> UIView {
    let nibName:String = "customView"
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: nibName, bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView // **Getting error at this line**
    return view
}
}

customView.xib

文件的所有者是 CustomView 类。

在故事板中

我有一个 UIViewController(滚动视图控制器)和一个带有自定义类的 UIView:CustomView。 xib @IBDesignable 传播通过,一切都很好。

现在问题开始了。我试图在 ScrollViewController 中多次使用 customView(就像 tableView 中的单元格),就像我使用 viewOne 一样

MyUIViewController.swift

import UIKit


class MyUIViewController: UIViewController,UIScrollViewDelegate {

let scrollView = UIScrollView()
let height = CGFloat(234.0)

override func viewDidLoad() {
    super.viewDidLoad()

    self.scrollView.frame = CGRectMake(0, 150, self.view.bounds.size.width, height)
    self.scrollView.contentSize = CGSizeMake(height*3,self.view.bounds.size.width)
    self.scrollView.backgroundColor = UIColor.redColor()
    self.view.addSubview(self.scrollView)

    var y = CGFloat(0.0)

    for i in 0..<2 {

        //Adding viewOne
        let viewOne = self.createViewOne(i)
        viewOne.frame = CGRectMake(0, y, 320, 200)
        self.scrollView.addSubview(viewOne)

        //Adding CustomView
        let customView = self.createCustomView(i)
        customView.frame = CGRectMake(0, y, 320, 200)
        self.scrollView.addSubview(customView)

        y += height
    }

}

func createViewOne(index: Int) -> UIView {
    let viewOne = UIView()

    if index == 0{
        viewOne.backgroundColor = UIColor.greenColor()
    }

    if index == 1{
        viewOne.backgroundColor = UIColor.redColor()
    }

    return viewOne
}


func createCustomView(index: Int) -> UIView {

    let customViewDummy: customView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0] as! customView

    if index == 0{
        customViewDummy.backgroundColor = UIColor.greenColor()
    }

    if index == 1{
        customViewDummy.backgroundColor = UIColor.redColor()
    }

    return customViewDummy
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

我试图在 ViewController 中多次从 nib 加载自定义 UIView。

我参考了以下链接 - How to use custom nib view in ViewController multiple times

我正在使用 Xcode 7.3.1

感谢任何建议。提前谢谢。

【问题讨论】:

  • 不要假设 [0] as! UIView 的所有内容都正确,而是检查数组是否有任何内容,如果有,检查 [0] 是否具有您期望的对象类型。

标签: swift uiview uiscrollview subclass xib


【解决方案1】:

您正在您的customView 中再创建一个view 并将其添加为subView。因此有些不匹配。试试下面的代码,确保你的filesOnwer 设置为customView

@IBDesignable class customView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        loadViewFromNib()
    }

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

        loadViewFromNib()
    }

    func loadViewFromNib() -> UIView {
        let myView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil).first as! UIView
    return myView
    }
}

【讨论】:

  • 感谢您的回复。但是我在线上遇到了同样的错误 - let myView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil).first as!界面视图
猜你喜欢
  • 2013-09-24
  • 2012-12-28
  • 2019-01-14
  • 2014-12-24
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多