繁体   English   中英

添加 UIViewContainer 后出现意外的子视图

[英]Unexpected Subview after adding UIViewContainer

我有一个 ViewController,在那里我有一个 Table View,它从结构中加载数据,一切都很好。 我添加了一个分段控制器,然后创建了 2 个 UIViewContainers。

我将所有 Tableview 代码移动到 UIViewContainer 1 并且 UIViewContainer 2 当前为空。 我还将故事板中的 TableView 移到了新的 UIViewContainer 1

在代码 velow UIViewcontainer 1 = tableContainer UIViewController 2 = mapContainer

我已经为分段视图控制器添加了代码,如下所示:

@IBOutlet weak var mapContainer: UIView!
@IBOutlet weak var tableContainer: UIView!


@IBAction func showContainer(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        UIView.animate(withDuration: 0.5, animations: {
            self.tableContainer.alpha = 1
            self.mapContainer.alpha = 0
        })

    }else {
        UIView.animate(withDuration: 0.5, animations: {
            self.tableContainer.alpha = 0
            self.mapContainer.alpha = 1
        })
    }

} 

这或多或少是我的主视图控制器中唯一的代码。

当我运行时,编译工作正常,但在加载时崩溃并出现以下错误:

*** Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 'There are unexpected 
subviews in the container view. Perhaps the embed segue has already 
fired once or a subview was added programmatically?'
***

我是否错过了我需要对 tableView 或一般视图做的事情?

日志说容器视图中有意外的子视图。 您可能希望确保所有子视图都移动到(嵌入)segueD UIViewController 而不是容器本身。

我现在已经解决了这个问题。 设置时一定出了点问题,我删除了所有容器并将应用程序恢复原状,重新启动,现在工作正常

我的问题是我不小心在prepare(for:sender:)执行了segue.perform() 一旦我删除了该行,错误就消失了。

就我而言,这是解决方案。 鉴于您的容器视图称为containerView ,请致电

containerView.removeSubviews()

在你打电话之前

performSegue(withIdentifier: "YourSegueID", sender: self)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM