处理作为文件所有者的对象的awakeFromNib [英] Handling of awakeFromNib for the object that is File's Owner

查看:106
本文介绍了处理作为文件所有者的对象的awakeFromNib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多相关问题,为什么在实例化某些视图时未调用awakeFromNib. 从Nib唤醒特定视图的消息将发送到视图本身,而该消息不会传递给文件的所有者. 我看到了为什么我的awakeFromNib不会开火?.

I know there are many related questions why awakeFromNib is not called when instantiating some view. The message that the certain view is awaken from Nib is sent to the view itself and this message is not delivered to the File's Owner. I saw Why won't my awakeFromNib fire?.

那么,如果您创建一个视图实例,该实例的文件所有者本身在xib文件中,会发生什么情况?

So, what will happen if you create an view's instance whose File's Owner is itself in the xib file?

换句话说,您拥有自己的名为MyCustomView.swift和MyCustomView.xib的自定义视图.然后在xib文件中,将文件的所有者设置为MyCustomView. 因此,当您创建MyCustomView的实例时,是否会调用awakeFromNib? 就我而言,awakeFromNib似乎没有被调用. 但是,视图本身确实是实例化的.因此,对我来说,awakeFromNib没有被调用是很奇怪的.

In other word, you have your own custom view named MyCustomView.swift and MyCustomView.xib. And in the xib file, you set the file's owner to MyCustomView. So, when you create the instance of MyCustomView, does awakeFromNib get called? In my case, the awakeFromNib doesn't seem to be called. However, the view itself is really instantiated. So, to me it is strange that the awakeFromNib is not called.

任何人都可以解释这件事吗?

Could anyone explain this thing?

仅供参考: 我准备了BaseCustomView.swift. BaseCustomView有两个init.

FYI: I prepared BaseCustomView.swift. BaseCustomView has two init.

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

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

commonInit()就是这样.

private func commonInit() {
        // load custom view's xib
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: self.className(), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        addSubview(view)

        // make custom view's size the same size with itself
        view.translatesAutoresizingMaskIntoConstraints = false
        let bindings = ["view": view]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|",
            options:NSLayoutFormatOptions(rawValue: 0),
            metrics:nil,
            views: bindings))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|",
            options:NSLayoutFormatOptions(rawValue: 0),
            metrics:nil,
            views: bindings))
    }

而customView只是覆盖此BaseCustomView的类. 另外,customView的文件所有者是customView本身.

And customView is just the class which overrides this BaseCustomView. In addition, the customView's File's Owner is customView itself.

更多 自定义视图类是这样的.实际上,awakeFromNib()没有被调用.

More The custom view class is like this. And actually the awakeFromNib() is not called.

final class MyCustomView: BaseCustomView {

    override func awakeFromNib() {
    // do something
    }

}

推荐答案

基于Josh和Kazuya的评论,xib中的File Owner显然位于Placeholder下,而正是这样,编译器知道存在一个ivar的占位符实例化后,该XIB上的内存中将有一个对象可以连接.实际上,如果将其保留为空白,则AppDelegate拥有它.我认为File的Owner现在在IB中确实已经过时了.

Building on Josh and Kazuya's comments, File's Owner in a xib is obviously located under Placeholder and it is exactly that, a placeholder for the compiler to know there's an ivar on that xib that will have an object in memory to connect to once the xib is instantiated. And actually if you leave it blank, the AppDelegate owns it. File's Owner in my opinion is really obsolete nowadays in IB.

在右侧的检查器中,如果将文件的所有者"->自定义类"->类"设置为MyCustomView,但未将自定义类"->"XIB的顶级层次结构UIView的类"设置为"MyCustomView",则IBOutlet将赢得不会被连接,而awakeFromNib()不会被调用.

In the inspector on the right hand side, if you set File's Owner -> Custom Class -> Class to MyCustomView but don't set Custom Class -> Class of the xib's top-level hierarchy UIView to MyCustomView, then IBOutlet won't be connected and awakeFromNib() won't be called.

所附图像显示了必须在何处设置MyCustomView,以便调用任何IBOutlets和awakeFromNib()以及MyCustomView.swift中的任何其他方法

Image attached shows where MyCustomView MUST be set in order for any IBOutlets and awakeFromNib() as well as any other methods in MyCustomView.swift to be called

这篇关于处理作为文件所有者的对象的awakeFromNib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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