gpt4 book ai didi

swift - 当从 xib 加载 subview 到 UIScrollView 时,快速出现错误 'EXC_BAD_ACCESS'

转载 作者:行者123 更新时间:2023-11-30 13:04:01 25 4
gpt4 key购买 nike

我正在尝试从 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 类。

在 Storyboard中

我有一个 UIViewController( ScrollView Controller ),其中包含一个带有自定义类的 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

如有任何建议,我们将不胜感激。提前谢谢您。

最佳答案

您将在 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
}
}

关于swift - 当从 xib 加载 subview 到 UIScrollView 时,快速出现错误 'EXC_BAD_ACCESS',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39594695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com