如何在ViewController中多次使用自定义笔尖视图 [英] How to use custom nib view in ViewController multiple times

查看:94
本文介绍了如何在ViewController中多次使用自定义笔尖视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to load a custom UIView from nib

CustomView.swift

CustomView.swift

import 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 {
        var nibName:String = "CustomView"
        var bundle = NSBundle(forClass: self.dynamicType)
        var nib = UINib(nibName: nibName, bundle: bundle)

        var view = nib.instantiateWithOwner(self, options: nil)[0] as UIView
        return view
    }

}

CustomView.xib

CustomView.xib

文件的所有者是CustomView类.

File's Owner is the CustomView class.

在StoryBoard中

In StoryBoard

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

I have a UIViewController (Scroll View Controller) with a UIView with the custom class: CustomView. The xib @IBDesignable propagates through and all is well.

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

Now the problem starts. I am trying to use the customView multiple times (just like cells in a tableView) in the ScrollViewController, just as I do with viewOne

ScrollViewController.swift

ScrollViewController.swift

import UIKit

class ScrollViewController: UIViewController {

    let scrollView = UIScrollView()


    override func viewDidLoad() {
        super.viewDidLoad()

        let height = CGFloat(200.0)

        self.scrollView.frame = self.view.bounds
        self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, height*3)
        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 customView: CustomView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as CustomView  --> Code breaks here

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

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

        return customView
    }


}

问题 代码在下面的行中断,控制台输出无用(lldb)

Question the code breaks at the line below and console output is unhelpful (lldb)

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

我也尝试用以下代码实例化:

I have also tried to instantiate with this code:

customView = CustomView.loadViewFromNib()

但随后出现错误呼叫中参数#1缺少参数"

but then I get error "Missing argument for parameter #1 in call"

1)如何在ViewController中多次从笔尖加载自定义UIView. 2)如何更改视图中包含的UI元素,例如UIImageView,UILabels等.如何设置标题如customView.title.text ="Fido"

1) How can I load a custom UIView from nib multiple times in ViewController. 2) How can I change the UI Elements contained within the view, such as UIImageView, UILabels etc. E.g. how can I set the title like customView.title.text = "Fido"

任何帮助将不胜感激!谢谢.

Any help would be very much appreciated ! Thank You.

推荐答案

您的视图类的全部要点是它将加载笔尖并将创建的视图添加为子视图.它也被定义为笔尖的所有者.

The whole point of your view class is that it loads the nib and adds the created view as a subview. It is also defined as the owner of the nib.

因此,当您的视图控制器尝试以自身作为所有者加载笔尖时,它会调用所有出口设置程序,但是它没有实现它们,因此会中断.

So, when your view controller tries to load the nib with itself as the owner it has all the outlet setters called on it, but it doesn't implement them, so it breaks.

要解决此问题,您应该分配并初始化视图,而不是从视图控制器中的nib中明确加载它.

To fix, you should be alloc and initing yhe view, not loading it from the nib explicitly in the view controller.

替换

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

使用

let customView = CustomView()

这篇关于如何在ViewController中多次使用自定义笔尖视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆