【问题标题】:Exc_Bad_Access While loading CustomView from XibExc_Bad_Access 从 Xib 加载 CustomView 时
【发布时间】:2017-12-16 11:43:32
【问题描述】:

CustomView 中的 IBOutlets 将为零。

我已经创建了自定义视图(xib)。

请查找图片以获取更多信息。

class TextFieldView: UIView {

@IBOutlet var contentView: TextFieldView!
@IBOutlet weak var customTextField: UITextField!
@IBOutlet weak var rightButton: UIButton!
@IBOutlet weak var placeHolderLabel: UILabel!

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

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

override func awakeFromNib() {
    super.awakeFromNib()
}

func commonInit() 
{
let bundle = Bundle(for: type(of: self))
bundle.loadNibNamed("TextFieldView", owner: self, options: nil)
customTextField.backgroundColor = UIColor.green
NSLog("Called")
}

exc_bad_access 仍然抛出错误。(在 loadNibNamed 代码行中)

【问题讨论】:

  • 什么图片?您是否在 XIB 中连接了这些插座?

标签: ios swift exc-bad-access iboutlet custom-view


【解决方案1】:

Swift 3 版本

这是对我有用的解决方案。确保您在 IB 中的文件对象具有分配给它的 TextFieldView 类(而不是 UIView 本身)。还要确保您的所有 IBOutlets 都连接到正确的位置,否则您可能会遇到麻烦。

class TextFieldView: UIView {

    @IBOutlet var contentView: TextFieldView!
    @IBOutlet weak var customTextField: UITextField!
    @IBOutlet weak var rightButton: UIButton!
    @IBOutlet weak var placeHolderLabel: UILabel!


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

                commonInit()
            }

        //This lets us use TextFieldView in IB
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }

        private func commonInit() {

                 Bundle(for: TextFieldView.self).loadNibNamed("TextFieldView", owner: self, options: nil)
                //Positioning content so it fills view space
                contentView.frame = self.bounds
                contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
                addSubview(contentView)
            }

【讨论】:

    【解决方案2】:

    你需要这样的东西:

    /// This view must be connected with the main view of the XIB.
    @IBOutlet private var view: UIView!
    
    /// This method is used when creating an `ApexView` with code.
    override init(frame: CGRect)
    {
        super.init(frame: frame)
    
        initView()
    }
    
    /// This method is called when instantiated from a XIB.
    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    
        initView()
    }
    
    private func initView()
    {
        /// Fill in the name of your XIB. I assumed it `"TextFieldView"`.
        Bundle.main.loadNibNamed("TextFieldView", owner: self, options: nil)
    
        self.view.frame = CGRect(x: 0.0, y: 0.0, width: self.width, height: self.height)
        self.addSubview(self.view!)
    
        ...
    }
    

    【讨论】:

    • 不要使用Bundle.main。使用Bundle(for: class)
    • @Sulthan 为什么不呢?Bundle(for: class) 的使用会怎样?
    • Bundle(for: type(of: self)),您会明白,当您开始使用从 IB 加载的设计对象时,它们的主包是不同的。
    • @Sulthan 随便。
    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多