美文网首页
iOS error: A view can only be as

iOS error: A view can only be as

作者: 舒小妮儿 | 来源:发表于2018-07-20 16:20 被阅读104次

    今天写个swift的demo,很久没用xib了,然后就遇到这个问题:

    2018-07-20 15:39:33.616032+0800 swiftLearn[10119:322872] Unknown class _TtC10swiftLearn14ViewController in Interface Builder file.
    2018-07-20 15:39:33.631203+0800 swiftLearn[10119:322872] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0x7f8a964149a0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x60000023d4a0>> is associated with <swiftLearn.ChouTiVC: 0x7f8a964152c0>. Clear this association before associating this view with <swiftLearn.ChouTiVC: 0x7f8a96703040>.'
    

    我检查了我的xib,File's Owner已经设置为我希望绑定的类名为MyVC



    发现xib的结构是:最外层是控制器MyVC,包了一层默认的View,这个是associated with the MyVC,但File's Owner又设置了view的关联,导致冲突。

    回顾下创建xib的过程,新建File选择View或者empty类型,前者会可视化界面生成一个View,后者需要自行拖入View。在实际操作中,我选择了empty,接着拖了个VC,太久没有使用xib布局了。

    所以,解决方案只需在可视化界面完成以下两步,即为下图所示,然后clean再run就ok了。
    1、将MyVC中最外层View独立出来
    2、删除MyVC控制器(确保正确设置过File's Owner)

    其实这个在14年刚开始学习可视化界面布局时,都不会犯这样都错误,但时间一长,习惯使用代码布局之后,这些细枝末节的问题就容易被遗忘,故在此记录,以防再次掉坑。

    相关文章

      网友评论

          本文标题:iOS error: A view can only be as

          本文链接:https://www.haomeiwen.com/subject/fqkqmftx.html