UIPageControl内のXIBからカスタムビューをロードしようとしていますが、ビューをインスタンス化しようとしているときにEXC_BAD_ACCESSを取得しようとしています。 Plsは私がどこに間違っているかを案内します。ここでUIPageControl内でXIBからカスタムビューをロードしようとしたときに発生する問題
は私のコードです:
私は、スクロールビューとページのコントロールを持っているVCは、ストーリーボードを経由して追加しました。私は、ページに応じて、ページコントロール内の異なるデータを持つビューをロードしたいと思います。
カスタムXIB:
私は、ビューを持っており、いくつかのラベルやイメージビューは制約で、同じの上に追加のカスタムXIBを、持っています。 xibファイルはカスタムビュークラスを使用するように更新され、アウトレットもマップされます。
カスタムビュー:
import UIKit
class EventPageControl: UIView {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var eventIcon: UIImageView!
@IBOutlet weak var eventDescLabel: UILabel!
class func instanceFromNib() -> UIView {
let pageControl = UINib(nibName: "EventPageControl", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
return pageControl
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// MARK: - Private Methods
// Performs the initial setup.
fileprivate func setupView() {
let view = viewFromNibForClass()
view.frame = bounds
view.autoresizingMask = [
UIViewAutoresizing.flexibleWidth,
UIViewAutoresizing.flexibleHeight
]
addSubview(view)
}
// Loads a XIB file into a view and returns this view.
fileprivate func viewFromNibForClass() -> UIView {
let bundle = Bundle(for: type(of: self))
//let bundle = Bundle(forClass: type(of: self))
//let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nib = UINib(nibName: "EventPageControl", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
のViewController:
func configureScrollView() {
// Enable paging.
var scrollWidth : CGFloat = UIScreen.main.bounds.size.width - 20.0
scrollView.isPagingEnabled = true
// Set the following flag values.
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.scrollsToTop = false
// Set the scrollview content size.
scrollView.contentSize = CGSize(width: scrollWidth * CGFloat(totalPages), height: scrollView.frame.size.height)
// Set self as the delegate of the scrollview.
scrollView.delegate = self
// Load the view from the xib file and configure it properly.
for i in 0...(totalPages - 1) {
//let eventPageControl = EventPageControl.instanceFromNib()
let eventPageControl = EventPageControl.init(frame: CGRect(x: 0, y: 0, width: scrollWidth, height: scrollView.frame.size.height))
// update the event icon and event desc properties of the custom view here
// Set its frame
eventPageControl.frame = CGRect(x: CGFloat(i) * scrollWidth, y: 0, width: scrollWidth, height: scrollView.frame.size.height)
// Add the test view as a subview to the scrollview.
scrollView.addSubview(eventPageControl)
}
}
例外:カスタムビューのコード機能でラインlet view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
上
EXC_BAD_ACCESS - viewFromNibF orClass()
私はビューコントローラでは、代わりにlet eventPageControl = EventPageControl.init(frame: CGRect(x: 0, y: 0, width: scrollWidth, height: scrollView.frame.size.height))
の、let eventPageControl = EventPageControl.instanceFromNib()
を使用しようとすると、それはそうと、ページコントロール内の図を示しているが、カスタムビュー内のプロパティへのハンドルを取得することはできませんよビューコントローラでここでそれらを更新することができます。
Plsは問題の修正にお手伝いします。より詳細が必要な場合は、私に連絡してください。
@BaShaような何か、あなたはplsは助けることができますか? – csharpnewbie