尝试从UIPageControl中的XIB加载自定义视图时出现问题

时间:2017-07-21 17:52:33

标签: ios swift3 storyboard xib uipagecontrol

我试图在UIPageControl中从XIB加载自定义视图,但在尝试实例化视图时获取EXC_BAD_ACCESS。请指导我哪里出错了。

这是我的代码:

我有一个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 - viewFromNibForClass()

当我尝试使用let eventPageControl = EventPageControl.instanceFromNib()时,而不是let eventPageControl = EventPageControl.init(frame: CGRect(x: 0, y: 0, width: scrollWidth, height: scrollView.frame.size.height)) ,在视图控制器中,它显示页面控件内的视图,但是无法获取自定义视图中属性的句柄,因此我可以在视图控制器中更新它们。

请帮助我解决问题。如果您需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

如果在viewcontroller中添加自定义视图的私有实例,会发生什么?这是否可以让您访问视图的属性..?

private let instanceOfEPC = EventPageControl()

这样的东西