【问题标题】:Unable to Modify View Elements when Adding a SubView添加子视图时无法修改视图元素
【发布时间】:2018-04-19 03:36:57
【问题描述】:

背景:

我有 4 个 .XIB 文件,它们以编程方式初始化为要通过 self.view.addSubview(UIView) 方法调用到屏幕的子视图。

    //Initialize Associate Degree Selection UIView and prepare contraints
    degreeAuditAssociatesView = instanceFromNib(XIBFilename: "degreeAuditAssociate")
    degreeAuditAssociatesView?.frame = self.view.bounds
    degreeAuditAssociatesView?.autoresizingMask = [UIViewAutoresizing.flexibleWidth,UIViewAutoresizing.flexibleHeight]
    loadSavedCheckboxes()

    //Initialize Associate Degree Selection Results UIView and prepare contraints
    degreeAuditAssociatesViewResults = instanceFromNib(XIBFilename: "degreeAuditAssociateResults")
    degreeAuditAssociatesViewResults?.frame = self.view.bounds
    degreeAuditAssociatesViewResults?.autoresizingMask = [UIViewAutoresizing.flexibleWidth,UIViewAutoresizing.flexibleHeight]

    //Initialize Bachelors Degree Selection UIView and prepare contraints
    degreeAuditBachelorsView = instanceFromNib(XIBFilename: "degreeAuditBachelors")
    degreeAuditBachelorsView?.frame = self.view.bounds
    degreeAuditBachelorsView?.autoresizingMask = [UIViewAutoresizing.flexibleWidth,UIViewAutoresizing.flexibleHeight]
    loadSavedCheckboxes()

    //Initialize Bachelors Degree Selection Results UIView and prepare contraints
    degreeAuditBachelorsViewResults = instanceFromNib(XIBFilename: "degreeAuditBachelorsResults")
    degreeAuditBachelorsViewResults?.frame = self.view.bounds
    degreeAuditBachelorsViewResults?.autoresizingMask = [UIViewAutoresizing.flexibleWidth,UIViewAutoresizing.flexibleHeight]

当我调用 self.view.addSubview(degreeAuditBachelorsViewResults!) 时,视图会正确显示此 XIB UIView 并相应地填充所有文本框和显示元素。

问题: 当我调用 self.view.addSubview(degreeAuditAssociatesViewResults!)(第二个代码块)时,视图会显示此 XIB UIView,但 不会在文本框中填充任何数据。强>

有什么奇怪的 当我删除代码并且从不初始化第四个 UIView(第四个代码块)(degreeAuditBachelorsViewResults)时,degreeAuditAssociateResults UIView 加载没有问题。

当我在代码块中移动时,(块 2)或(块 4)都不会显示,所有文本元素都填充了信息。我感觉它与 addSubview(UIView) 方法有关,不接受任何信息。

请帮助,我已经用完了选项。

【问题讨论】:

  • loadSavedCheckboxes() 发生了什么?
  • 如果 UserDefaults.standard 键设置为 true,则该功能正在设置要启用的 UI 按钮。
  • 好吧 - 有点令人困惑...您是否同时加载所有这四个?所以它们相互重叠(它们的框架都设置为 view.bounds)? “填充文本框和显示元素”的代码在哪里?
  • 我刚刚有一个灯泡时刻。当你说它必须对复选框做一些事情时,它就被掩盖了。经过进一步调试,我发现了问题所在。在 xcode 中,最后一个被初始化的视图分配参考出口。看起来两个视图都引用了相同的插座,但最后一个视图是连接插座并断开其他视图的视图。这就是为什么只填充最后一块代码而没有第二块的原因。这已经解决了。谢谢唐马格!

标签: ios xcode swift4.1


【解决方案1】:

这是为了将来参考这个问题。如果两个视图具有相同的引用出口并且正在从 XIB 文件以编程方式初始化。最后初始化的 UI 视图将连接参考插座。每个视图都应尽可能引用其单独的出口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多